title: invalid_grant Code was already redeemed
body: <p>I have a PHP application which is managing Google calendar entries. Its installed on two of my sites.</p>
  
  <p>On the first site, it has been running for about a year, no problems and the sync is maintained.</p>
  
  <p>On the other site however (different Google account), I keep seeing the error...</p>
  
  <pre><code>Invalid token formatarray (
    'error' =&gt; 'invalid_grant',
    'error_description' =&gt; 'Code was already redeemed.',
  )
  </code></pre>
  
  <p>Been trying to figure this out for weeks now with no joy. Here are some snippets of my code (used within WordPress)...</p>
  
  <pre><code>$this-&gt;auth  = get_option( 'google_calendar_auth_code' ); // API Setting
  $this-&gt;token = get_transient( 'google_calendar_token' );
  
  $this-&gt;client = new Google_Client();
  $this-&gt;client-&gt;setApprovalPrompt( 'force' );
  $this-&gt;client-&gt;setAccessType( 'offline' );
  $this-&gt;client-&gt;setApplicationName( 'MY_APP' );
  $this-&gt;client-&gt;setClientId( '*****.apps.googleusercontent.com' );
  $this-&gt;client-&gt;setClientSecret( '******' );
  $this-&gt;client-&gt;setRedirectUri( 'urn:ietf:wg:oauth:2.0:oob' );
  $this-&gt;client-&gt;setScopes( array( 'https://www.googleapis.com/auth/calendar.readonly', 'https://www.googleapis.com/auth/calendar.readonly' ) );
  
  try {
      $this-&gt;calendar = new Google_Service_Calendar( $this-&gt;client );
  } catch( Google_Service_Exception $e )  {
      error_log( 'ERROR: ' . $e-&gt;getMessage() );
  }
  
  if ( ! empty( $this-&gt;token ) )  {
  
      $this-&gt;client-&gt;setAccessToken( $this-&gt;token );
  
  } else  {
  
  try {
  
      $access_token = $this-&gt;client-&gt;authenticate( $this-&gt;auth );
  
  } catch( Exception $e ) {
  
      error_log( 'Unable to connect to calendar. ' . $e-&gt;getMessage() );
  
      return false;
  
  }
  
  if ( $access_token )    {
  
      try {
          $this-&gt;client-&gt;setAccessToken( $access_token );
      } catch( Exception $e ) {
          error_log( 'Access token error. ' . $e-&gt;getMessage() . var_export( $access_token, true ) );
          return false;
      }
  
      set_transient( 'google_calendar_token', $access_token, 0 );
  
      $redirect = admin_url( 'edit.php' );
  
      wp_safe_redirect( $redirect );
      exit;
  
      } else  {
          return false;
      }
  
      $this-&gt;token = json_decode( $this-&gt;client-&gt;getAccessToken() );
  
      if ( ! defined( 'GOOGLE_TOKEN' ) )  {
          define( 'GOOGLE_TOKEN', true );
      }
  
      return true;
  
  }
  </code></pre>
  
  <p>I'm clearly not refreshing the token correctly or something but I cannot figure it out.</p>
  
  <p>Any help appreciated.</p>