我使用youtube API將視頻從我的網站上傳到我的YouTube帳戶。 YouTube API每次都要求訪問我的帳戶的API。youtube api要求我每次訪問我的帳戶的api
有什麼辦法來問只是一個時間,並保存帳戶數據,並沒有爲下一次上傳再問,請幫我
,這是我的代碼:
$client = new \Google_Client();
$OAUTH2_CLIENT_ID = 'xxxxxxxxx';
$OAUTH2_CLIENT_SECRET = 'xxxxxxxx';
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setAuthConfig('xxxxxxxx/'.client_secrets.json');
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setAccessType("offline");
$client->setIncludeGrantedScopes(true);
$client->setRedirectUri('http://myWebsiteUrl');
if (!isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
exit;
} else{
$title = 'title';
$descriptions = 'description';
}
$client->authenticate($_GET['code']);
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$_SESSION['access_token'] = $client->getAccessToken();
}
try {
$youtube = new \Google_Service_YouTube($client);
$videoPath = 'video path';
$snippet = new \Google_Service_YouTube_VideoSnippet();
$snippet->setTitle($title);
$snippet->setDescription($descriptions);
$snippet->setCategoryId('22');
$status = new \Google_Service_YouTube_VideoStatus();
$status->privacyStatus = "public";
$video = new \Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$insertRequest = $youtube->videos->insert("status,snippet", $video);
$media = new \Google_Http_MediaFileUpload($client, $insertRequest, 'video/*', null, true, $chunkSizeBytes);
$media->setFileSize(filesize($videoPath));
$status = false;
$handle = fopen($videoPath, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
$client->setDefer(false);
} catch (Google_Service_Exception $e) {
echo $e->getMessage();
} catch (Google_Exception $e) {
die('error');
}
我成立了這一點,我可以上傳視頻,但我的問題是,YouTube要求每個訪問API我的帳戶的時間。我希望youtube提問一次並保存我的訪問數據以供下次上傳。 – vm30