title: Unable to refresh OAuth2 token in PHP, invalid grant
body: <p>I need to make a PHP script that creates a sigle event on Google Calendar.
  I had no problems setting up client id, client secret, dev key and creating a new event.</p>
  
  <p>My only problem is with OAuth2, in particular I need to make a permanent connection and I do not want to do the authentication everytime I run the script.</p>
  
  <p>Actually, with this script I'm able to get a token and a refresh token, but every hour my token expires and I don't know how to refresh it. How can I edit this code to do that?
  Can I save both the token and the refresh token somewhere and always use the same data?</p>
  
  <p>I obtain an uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" </p>
  
  <p>I have already read some other posts about this topic here in stackoverflow but I still haven't found a solution... :\</p>
  
  <pre><code>&lt;?php
  
   require_once 'src/Google_Client.php';
   require_once 'src/contrib/Google_CalendarService.php';
   session_start();
  
   $client = new Google_Client();
   $client-&gt;setApplicationName("Calendar App");
   $client-&gt;setClientId('xxxx');
   $client-&gt;setClientSecret('yyy');
   $client-&gt;setRedirectUri('http://www.zzzzzz');  
   $client-&gt;setDeveloperKey('kkk');
   $client-&gt;setAccessType('offline');
   $cal = new Google_CalendarService($client);    
  
  if (isset($_GET['logout'])) {
    unset($_SESSION['token']);
  }
  
  if (isset($_GET['code'])) {
    $client-&gt;authenticate($_GET['code']);
    $_SESSION['token'] = $client-&gt;getAccessToken();
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); 
  }
  
  if (isset($_SESSION['token'])) {
      //echo $_SESSION['token'];
    //$client-&gt;setAccessToken($_SESSION['token']);
      $authObj = json_decode($_SESSION['token']);
      $accessToken = $authObj-&gt;access_token;
      $refreshToken = $authObj-&gt;refresh_token;
      $tokenType = $authObj-&gt;token_type;
      $expiresIn = $authObj-&gt;expires_in;
      echo 'access_token = ' . $accessToken;
      echo '&lt;br /&gt;';
      echo 'refresh_token = ' . $refreshToken;
      echo '&lt;br /&gt;';
      echo 'token_type = ' . $tokenType;
      echo '&lt;br /&gt;';
      echo 'expires_in = ' . $expiresIn;
  }
  
  if(!empty($cookie)){
      $client-&gt;refreshToken($this-&gt;Cookie-&gt;read('token'));
  }
  
  if ($client-&gt;getAccessToken()) {
      $calList = $cal-&gt;calendarList-&gt;listCalendarList();
      $_SESSION['token'] = $client-&gt;getAccessToken();
  } else {
    $authUrl = $client-&gt;createAuthUrl();
    print "&lt;a class='login' href='$authUrl'&gt;Connect Me!&lt;/a&gt;";
  }
  
  
  // Creation of a single event
  $event = new Google_Event();
  $event-&gt;setSummary($event_name);            
  $event-&gt;setLocation('');                    
  ....
  
  ?&gt;
  </code></pre>
  
  <p>Thanks a lot for your support!</p>