Youtube API Token Expire while long time upload

≯℡__Kan透↙ 提交于 2019-12-11 08:07:37

问题


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

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

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)

This is the upload 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);

        }

My work Around at the moment: <meta http-equiv="refresh" content="600">

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);

This is new:

$client->setAccessType("offline");
$client->setApprovalPrompt("force");

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


How can i refresh the token while uploading?

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]);
            }
        }

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!


回答1:


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



来源:https://stackoverflow.com/questions/47860441/youtube-api-token-expire-while-long-time-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!