title: Authentication on google: OAuth2 keeps returning 'invalid_grant'
body: <p>I started to configure google calendar on my new application. I almost made an exact copy of the authentication code displayed at google developers ( <a href="https://developers.google.com/google-apps/calendar/instantiate" rel="noreferrer">https://developers.google.com/google-apps/calendar/instantiate</a> ), but i keep getting the following error:</p>
  
  <p>Error fetching OAuth2 access token, message: 'invalid_grant' </p>
  
  <p>I'm currently using Fork-CMS ( <a href="http://www.fork-cms.com" rel="noreferrer">http://www.fork-cms.com</a> ), a young lightweigth CMS. I correctly configured the config.php file of the google-api-php-client. (client-id, client-secret, redirect-uri, development key,...) and the redirect uri is correctly set on the google api's console. My code looks as follows:</p>
  
  <pre><code>&lt;?php
  
  /**
  * This is a widget with a calendar implementation.
  *
  * @package       frontend
  * @subpackage    events
  *
  * @author        Michiel Vlaminck &lt;michielvlaminck@gmail.com&gt;
  */
  class FrontendEventsWidgetCalendar extends FrontendBaseWidget
  {
  
      private $events = array();
      private $authUrl = array();
  
      /**
      * Execute the extra
      *
      * @return    void
      */
      public function execute()
      {      
          // call parent
          parent::execute();
  
          // load template
          $this-&gt;loadTemplate();
  
          // get data
          $this-&gt;getData();
  
          // parse
          $this-&gt;parse();
      }
  
  
      /**
      * Get the data from Google Calendar
      * This method is only executed if the template isn't cached
      *
      * @return    void
      */
      private function getData()
      {
          require_once PATH_LIBRARY . '/external/google-api-php-client/src/apiClient.php';
          require_once PATH_LIBRARY . '/external/google-api-php-client/src/contrib/apiCalendarService.php';
  
          $client = new apiClient();
  
          $service = new apiCalendarService($client);
  
          if (isset($_SESSION['oauth_access_token'])) {
              $client-&gt;setAccessToken($_SESSION['oauth_access_token']);
          } else {
              $token = $client-&gt;authenticate();
              $_SESSION['oauth_access_token'] = $token;
          }
  
          if ($client-&gt;getAccessToken()) {
  
              $calId = FrontendEventsModel::getCalendarId((int) $this-&gt;data['id']);
              $calId = $calId[0]['calendar_id'];
  
              $events = $service-&gt;events-&gt;listEvents($calId);
              $this-&gt;events = $events['items'];
  
              $_SESSION['oauth_access_token'] = $client-&gt;getAccessToken();
  
          } else {
              $this-&gt;authUrl = $client-&gt;createAuthUrl();
          }
      }
  
  
      /**
      * Parse
      *
      * @return    void
      */
      private function parse()
      {
          $this-&gt;tpl-&gt;assign('events', $this-&gt;events);
          $this-&gt;tpl-&gt;assign('authUrl', $this-&gt;authUrl);
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>When I open this widget-page for the first time, I get directed to google to authenticate the application. When I agree, I get redirected to my application and that's the point where I'm getting:</p>
  
  <pre><code>apiAuthException » Main
  
  Message Error fetching OAuth2 access token, message: 'invalid_grant'
  File    C:\wamp\www\Officevibes\library/external\google-api-php-client\src\auth\apiOAuth2.php
  Line    105
  Date    Thu, 05 Apr 2012 08:34:47 +0000
  URL http://localhost/calendar?code=4/YPUpFklKvhEeTcMm4moRth3x49oe
  Referring URL   (Unknown)
  Request Method  GET
  User-agent  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19
  </code></pre>