长时间上传后,Youtube API令牌过期 [英] Youtube API Token Expire while long time upload

查看:105
本文介绍了长时间上传后,Youtube API令牌过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下脚本将视频上传到我的YouTube频道. https://github.com/youtube/api-samples/blob/master/php/resumable_upload.php

I am using following script to upload a video to my youtube Channel. https://github.com/youtube/api-samples/blob/master/php/resumable_upload.php

现在一切正常,除非我要上传大视频,而上传视频大约需要1.5-2.5小时.

Everything works fine now, except if i want to upload a large video what takes around 1,5 - 2,5 hours upload.

在上传过程中,令牌在60分钟后过期,看来脚本在上传后停止工作.

During the Upload, the Token Expire after 60 minutes and it seems the script stops working after upload.

因此,上传循环停止后计划的所有内容(缩略图,下一个视频等)

So everything what is planed after the upload loop stops working (thumbnail, next video, etc)

这是上传循环:

        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分钟重新加载一次,再次获得令牌并恢复上传.

So yeah the site reload every 10 minutes, get token again and resume the upload.

我认为这是处理此问题的一种不好的方法,但我并不真正理解会话"和令牌"以及此类内容.有没有更好的方法来制作长寿令牌或刷新令牌?

I Think this is a bad way to handle this but i do not realy understand "session" and "token" and this stuff. Is there a better way to make a long live token or refresh the token?

我已经准备好进行此更改,因此我在令牌数组中获得了刷新令牌:

I allready made this change so i got a refresh token in token array:

$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"循环中

That seems to give me a refresh token, but i do not know how to handle it or put a refresh command into this "while" loop

上传时如何刷新令牌?

我首先想到的是将该脚本的开头重新放置到while循环中的令牌中:

my first thought was to put the start of this script where i get the token again into while loop:

        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次!

But this cant be correct?! it would start to get the token every 1MB Chunk. That would mean around 5000 times while uploading the video!

推荐答案

我自己发现-OMG -.-

OMG i found out by my self -.-

我必须在php ini中设置"session.cache_expire" ...

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

它设置为session.cache_expire=3600

什么意思是60分钟...

What means 60 mins...

i更改为session.cache_expire=68400<一天...现在,它在1小时后仍在上传:D

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

可以确认它是否有效:)晚上上传5个5-7GB的大视频

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

这篇关于长时间上传后,Youtube API令牌过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆