title: How do I refresh OAuth token?
body: <p>I am using Google Drive API and authenticating using OAuth token using PHP program. For my test program I went to OAuth url manually and created a token. Then, I used it in the program and saved it to the file. From next time onwards the token is loaded from file and the program is able to access Google Drive. However, since the token would only be valid for 1 hr, after that I get the error since the token it retrieves from the file won't be valid anymore. I am not clear on how I can refresh the token so that its not required to manually obtain a new token. </p>
  
  <p>Following is my code - </p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setClientId( CLIENT_ID ); 
  $client-&gt;setClientSecret( CLIENT_SECRET ); 
  $client-&gt;setRedirectUri( REDIRECT_URIS ); 
  $client-&gt;setScopes($SCOPES);
  $client-&gt;setAccessType('offline');
  $client-&gt;setAuthConfigFile(CLIENT_SECRET_PATH);
  </code></pre>
  
  <p>If the cred file exist, I pull the token from that file else I would be fetching it. For now, the first time token I am setting manually -</p>
  
  <pre><code>if (file_exists(CREDENTIALS_PATH)) {
      $accessToken = file_get_contents(CREDENTIALS_PATH);
    } else {
      $authCode = 'My Auth Code';
      $accessToken = $client-&gt;authenticate($authCode);
    }
    file_put_contents(CREDENTIALS_PATH, $accessToken);
  
  $client-&gt;setAccessToken($accessToken);
  </code></pre>
  
  <p>Now, if the token is expired I need to refresh the token automatically.</p>
  
  <pre><code>if ($client-&gt;isAccessTokenExpired()) {
      //This is where I am running in trouble.
  }
  </code></pre>
  
  <p>How do I achieve it? The target is to perform OAuth authentication and execute Google Drive API without any manual intervention.</p>
  
  <p>Thank you for the help!</p>