Authentication in google: OAuth2 continues to return 'invalid_grant'

I started customizing my Google calendar in my new app. I almost made an exact copy of the authentication code displayed by google developers ( https://developers.google.com/google-apps/calendar/instantiate ), but I keep getting the following error:

Error getting OAuth2 access token, message: 'invalid_grant'

I am currently using Fork-CMS ( http://www.fork-cms.com ), the young Lightweigth CMS. I set up the config.php file google-api-php-client correctly. (client-id, client-secret, redirect-uri, development key, ...), and uri redirection is set correctly on google api console. My code is as follows:

<?php /** * This is a widget with a calendar implementation. * * @package frontend * @subpackage events * * @author Michiel Vlaminck <michielvlaminck@gmail.com> */ 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->loadTemplate(); // get data $this->getData(); // parse $this->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->setAccessToken($_SESSION['oauth_access_token']); } else { $token = $client->authenticate(); $_SESSION['oauth_access_token'] = $token; } if ($client->getAccessToken()) { $calId = FrontendEventsModel::getCalendarId((int) $this->data['id']); $calId = $calId[0]['calendar_id']; $events = $service->events->listEvents($calId); $this->events = $events['items']; $_SESSION['oauth_access_token'] = $client->getAccessToken(); } else { $this->authUrl = $client->createAuthUrl(); } } /** * Parse * * @return void */ private function parse() { $this->tpl->assign('events', $this->events); $this->tpl->assign('authUrl', $this->authUrl); } } ?> 

When I open this widget page for the first time, I get a Google link to authenticate the application. When I agree, I am redirected to my application and that is the moment when I get:

 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 
+19
php google-calendar
Apr 05 '12 at 9:05
source share
5 answers

You must reuse the access token that you receive after the first successful authentication. You will receive an invalid_grant error if your previous token has not expired yet. Cache it somewhere so you can reuse it.

+22
Apr 17 '12 at 7:22
source share

I had a similar problem caused by the wrong time on my server. Make sure to synchronize your system clock.

+9
Nov 25 '13 at 16:45
source share

Go to the Google API console ( https://code.google.com/apis/console/ ) and cancel the client’s secret key under the client ID for installed applications . >.

Be sure to update your code with the new Client Secret

+8
Jul 10 2018-12-12T00:
source share
  • Go to security.google.com
  • Revoke access
  • again go to the authentication url.
  • now you will get a new refreshtoken
+3
Nov 10 '14 at 9:29
source share

You will also get this error if you mistakenly try to authenticate your identifier token , and not your access token .

So don’t be like me - make sure you pass the correct token to your code!

+1
Jan 07 '16 at 22:04
source share



All Articles