title: Automatically refresh token using google drive api with php script
body: <p>I followed again <a href="http://www.html.it/articoli/google-drive-e-php-come-integrarli/" rel="noreferrer">THIS TUTORIAL</a> to upload a file on Google Drive with php, directly from my REMOTE SERVER: so I have created new API Project from Google API Console, enabled Drive API service, requested OAuth Client ID and Client Secret, wrote them in a script, then upload it together with <a href="https://code.google.com/p/google-api-php-client/" rel="noreferrer">Google APIs Client Library for PHP</a> folder to this <a href="http://www.MYSERVER.com/script1.php" rel="noreferrer">http://www.MYSERVER.com/script1.php</a>, to retrieve Auth code:</p>
  
  <pre><code>&lt;?php
  
  require_once 'google-api-php-client/src/Google_Client.php';
  require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
  
  $drive = new Google_Client();
  
  $drive-&gt;setClientId('XXX'); // HERE I WRITE MY Client ID
  
  $drive-&gt;setClientSecret('XXX'); // HERE I WRITE MY Client Secret
  
  $drive-&gt;setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
  
  $drive-&gt;setScopes(array('https://www.googleapis.com/auth/drive'));
  
  $gdrive = new Google_DriveService($drive);
  
  $url = $drive-&gt;createAuthUrl();
  $authorizationCode = trim(fgets(STDIN));
  
  $token = $drive-&gt;authenticate($authorizationCode);
  
  ?&gt;
  </code></pre>
  
  <p>When I visit <a href="http://www.MYSERVER.com/script1.php" rel="noreferrer">http://www.MYSERVER.com/script1.php</a>  I <strong>allow authorization</strong> and get the Auth code that I can write in a second script. Then I upload it to <a href="http://www.MYSERVER.com/script2.php" rel="noreferrer">http://www.MYSERVER.com/script2.php</a>, who looks like:</p>
  
  <pre><code>&lt;?php
  
  require_once 'google-api-php-client/src/Google_Client.php';
  require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
  
  $drive = new Google_Client();
  
  $drive-&gt;setClientId('X');  // HERE I WRITE MY Client ID
  $drive-&gt;setClientSecret('X');  // HERE I WRITE MY Client Secret
  $drive-&gt;setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
  $drive-&gt;setScopes(array('https://www.googleapis.com/auth/drive'));
  
  $gdrive = new Google_DriveService($drive);
  
  $_GET['code']= 'X/XXX'; // HERE I WRITE AUTH CODE RETRIEVED AFTER RUNNING REMOTE script.php
  
  file_put_contents('token.json', $drive-&gt;authenticate());
  
  $drive-&gt;setAccessToken(file_get_contents('token.json'));
  
  $doc = new Google_DriveFile();
  
  $doc-&gt;setTitle('Test Drive');
  $doc-&gt;setDescription('Document');
  $doc-&gt;setMimeType('text/plain');
  
  $content = file_get_contents('drive.txt');
  
  $output = $gdrive-&gt;files-&gt;insert($doc, array(
        'data' =&gt; $content,
        'mimeType' =&gt; 'text/plain',
      ));
  
  print_r($output);
  
  ?&gt;
  </code></pre>
  
  <p>Well, now the file drive.txt is uploaded on my Google Drive and structure of token.json file is a sort of:</p>
  
  <pre><code>{"access_token":"XXX","token_type":"Bearer","expires_in":3600,"refresh_token":"YYY","created":1365505148}
  </code></pre>
  
  <p>Now, as you can imagine I can call script2.php and upload file until a certain time. Finally, the point is: I <strong>don't want</strong> the token to expire, I <strong>don't want to allow authorization</strong> each time it expire (recalling script1.php): I need to call the script2.php periodically during the day, to upload my file automatically, <strong>without user interaction</strong>. So, what's the best way to <strong>automatically refresh</strong> the token forever in this context? Do I need another script? Can I add some code to script2.php? or modify the token.json file? And where can I read the time remaining before the token expire? Thanks! </p>