title: PHP Google API Refresh Token
body: <p>Having a problem with the refreshToken procedure with Google API, upon an expired token is experienced the refreshToken doesn't get an AccessToken. I've already saved the refresh token to the db at this point.</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setApplicationName("APP NAME");
  $client-&gt;setClientId($client_id);
  $client-&gt;setClientSecret($client_secret);
  $client-&gt;setAccessType('offline'); 
  $client-&gt;setScopes(array('https://www.googleapis.com/auth/calendar'));
  $client-&gt;setAccessToken( json_encode($access_token_from_db) );
  
  if ( $client-&gt;isAccessTokenExpired() ) :
      $client-&gt;refreshToken($refresh_token_from_db);
      $new_access_token = $client-&gt;getAccessToken();
      print_r($access_token);
      //save to db -- but this fails
  endif;
  </code></pre>
  
  <p>After running this the new access token is still empty. Even if I setAccessToken to the now expired token prior to checking expiration the same problem persists.</p>
  
  <p>There doesn't seem to be a straight forward example on a state after a refreshToken has already been saved. </p>
  
  <p>In my application we would cron job to send updates to a user's calendar based on actions (if necessary to understand the application purpose).</p>
  
  <p>The reason this answer doesn't seem correct to me:
  <a href="https://stackoverflow.com/questions/9241213/how-to-refresh-token-with-google-api-client">How to refresh token with Google API client?</a></p>
  
  <p>is because it's not using the $client->isAccessTokenExpired() method, plus isn't a clean solution, the script should be very minimal and small to complete the task. In theory:</p>
  
  <ul>
  <li>setAccessToken to the one given on authorization</li>
  <li>if expired (via function), use the refresh to access a new token</li>
  <li>save the token and continue</li>
  </ul>
  
  <p>While the above example had similar functions it's a 2013 answer and may work but isn't the best way to do this I believe.</p>