title: Youtube API Token Expire while long time upload
body: <p>I am using following script to upload a video to my youtube Channel.
  <a href="https://github.com/youtube/api-samples/blob/master/php/resumable_upload.php" rel="nofollow noreferrer">https://github.com/youtube/api-samples/blob/master/php/resumable_upload.php</a></p>
  
  <p>Everything works fine now, except if i want to upload a large video what takes around 1,5 - 2,5 hours upload.</p>
  
  <p>During the Upload, the Token Expire after 60 minutes and it seems the script stops working after upload.</p>
  
  <p>So everything what is planed after the upload loop stops working (thumbnail, next video, etc)</p>
  
  <p>This is the upload loop:</p>
  
  <pre><code>        while (!$status &amp;&amp; !feof($handle)) {
  
              $chunk = fread($handle, $chunkSizeBytes);
              $status = $media-&gt;nextChunk($chunk);
  
              $sizecount = $sizecount+$chunkSizeBytes;
  
              $fp = fopen('./include/status.txt', "w");
              fwrite($fp, $sizecount);
              fclose($fp);
  
          }
  </code></pre>
  
  <p>My work Around at the moment: <code>&lt;meta http-equiv="refresh" content="600"&gt;</code></p>
  
  <p>So yeah the site reload every 10 minutes, get token again and resume the upload.</p>
  
  <p>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?</p>
  
  <p>I allready made this change so i got a refresh token in token array:</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setClientId($OAUTH2_CLIENT_ID);
  $client-&gt;setClientSecret($OAUTH2_CLIENT_SECRET);
  $client-&gt;setAccessType("offline");
  $client-&gt;setApprovalPrompt("force");
  $client-&gt;setScopes('https://www.googleapis.com/auth/youtube');
  $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
      FILTER_SANITIZE_URL);
  $client-&gt;setRedirectUri($redirect);
  </code></pre>
  
  <p>This is new:</p>
  
  <pre><code>$client-&gt;setAccessType("offline");
  $client-&gt;setApprovalPrompt("force");
  </code></pre>
  
  <p>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</p>
  
  <hr>
  
  <p>How can i refresh the token while uploading?</p>
  
  <p>my first thought was to put the start of this script where i get the token again into while loop:</p>
  
  <p>
  
  <pre><code>        while (!$status &amp;&amp; !feof($handle)) {
  
              $chunk = fread($handle, $chunkSizeBytes);
              $status = $media-&gt;nextChunk($chunk);
  
              $sizecount = $sizecount+$chunkSizeBytes;
  
              $fp = fopen('./include/status.txt', "w");
              fwrite($fp, $sizecount);
              fclose($fp);
  
              $client = new Google_Client();
              $client-&gt;setClientId($OAUTH2_CLIENT_ID);
              $client-&gt;setClientSecret($OAUTH2_CLIENT_SECRET);
              $client-&gt;setAccessType("offline");
              $client-&gt;setApprovalPrompt("force");
              $client-&gt;setScopes('https://www.googleapis.com/auth/youtube');
              $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
                  FILTER_SANITIZE_URL);
              $client-&gt;setRedirectUri($redirect);
  
  
              $youtube = new Google_Service_YouTube($client);
  
              $tokenSessionKey = 'token-' . $client-&gt;prepareScopes();
              if (isset($_GET['code'])) {
                  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
                      die('The session state did not match.');
                  }
  
                  $client-&gt;authenticate($_GET['code']);
                  $_SESSION[$tokenSessionKey] = $client-&gt;getAccessToken();
                  header('Location: ' . $redirect);
              }
  
              if (isset($_SESSION[$tokenSessionKey])) {
                  $client-&gt;setAccessToken($_SESSION[$tokenSessionKey]);
              }
          }
  </code></pre>
  
  <p>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!</p>