Youtube API令牌在长时间上传时过期

时间:2017-12-17 23:24:48

标签: php api youtube upload token

我正在使用以下脚本将视频上传到我的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次!

1 个答案:

答案 0 :(得分:2)

OMG i found out by my self -.-

i had to set "session.cache_expire" in php ini...

it was set to session.cache_expire=3600

What means 60 mins...

i changed to session.cache_expire=68400 < one day... noW it is still uploading after 1 hour :D

Hope someday someone could find this informationen helpfull

Edit: Can confirm it worked :) uploadet 5 Big 5-7GB videos over night