我使用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