【发布时间】:2018-05-31 07:44:17
【问题描述】:
我正在使用以下脚本将视频上传到我的 youtube 频道。 https://github.com/youtube/api-samples/blob/master/php/resumable_upload.php
现在一切正常,除非我想上传一个大视频,上传大约需要 1.5 - 2.5 小时。
在上传期间,令牌在 60 分钟后过期,并且脚本在上传后似乎停止工作。
所以上传循环停止工作后计划的一切(缩略图、下一个视频等)
这是上传循环:
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
$sizecount = $sizecount+$chunkSizeBytes;
$fp = fopen('./include/status.txt', "w");
fwrite($fp, $sizecount);
fclose($fp);
}
我现在的工作:<meta http-equiv="refresh" content="600">
是的,网站每 10 分钟重新加载一次,再次获取令牌并继续上传。
我认为这是处理此问题的不好方法,但我并不真正了解“会话”和“令牌”以及这些东西。有没有更好的方法来制作长寿令牌或刷新令牌?
我已经进行了这个更改,所以我在令牌数组中获得了一个刷新令牌:
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
这是新的:
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
这似乎给了我一个刷新令牌,但我不知道如何处理它或将刷新命令放入这个“while”循环中
上传时如何刷新令牌?
我的第一个想法是将这个脚本的开头放在我再次获取令牌的地方进入 while 循环:
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
$sizecount = $sizecount+$chunkSizeBytes;
$fp = fopen('./include/status.txt', "w");
fwrite($fp, $sizecount);
fclose($fp);
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$youtube = new Google_Service_YouTube($client);
$tokenSessionKey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION[$tokenSessionKey])) {
$client->setAccessToken($_SESSION[$tokenSessionKey]);
}
}
但这不可能是正确的?!它将开始每 1MB 块获取令牌。这意味着在上传视频时大约需要 5000 次!
【问题讨论】:
标签: php api youtube upload token