2

I have the following problem with authentication to Google API:

PHP Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /home/bot/bot/vendor/google/apiclient/src/Google/Auth/OAuth2.php:328

However, the issue is very strange, because same code works on my local PC and stops working on the server. Is it possible, that it has something to do with server time? I have my system clock synchronized, but server is about ~1 minute backwards. Seriously, I ran out of ideas why it doesn't work anymore though I haven't changed the code at all. It used to work before, but all of the sudden this problem appeared.

What can I do with that?

<?php
$client = new Google_Client();
$client->setApplicationName('My App');
$client->setClientId($clientId);
$client->setClassConfig(
    'Google_Cache_File',
    ['directory' => __DIR__.'/res/tmp']
);

$service = new Google_Service_Calendar($client);

$cred = new Google_Auth_AssertionCredentials(
    $serviceAccountName,
    // https://www.googleapis.com/auth/calendar
    [Google_Service_Calendar::CALENDAR],
    $keyFile
);
$client->setAssertionCredentials($cred);

var_dump($service->calendarList->listCalendarList());
stil
  • 4,876
  • 3
  • 35
  • 40
  • Show us the scope you are requesting – Jimmy Kane Jan 30 '14 at 17:47
  • I edited my post with sample code. It does work on my PC, and it doesn't on the server (but it used to). – stil Jan 30 '14 at 18:16
  • I suspect this : http://stackoverflow.com/questions/10025698/authentication-on-google-oauth2-keeps-returning-invalid-grant – Jimmy Kane Jan 30 '14 at 18:22
  • Unfortunately, after genererating private keys and even creating new client account - it works perfectly only on my computer, but same issue with the server. After some research, I found that Google answers with 400 HTTP code. – stil Jan 30 '14 at 18:31
  • If you solved your problem please post your answer and if possible update quite a few questions of users that have the same problem – Jimmy Kane Jan 30 '14 at 18:35
  • No, no. It works only on my computer, but not the server, where it is intended to be running. Problem is still occuring. – stil Jan 30 '14 at 18:37

1 Answers1

2

The solution was synchronizing the server time. Server time was only 40 seconds different to atomic clock, but after sending ticket to my hosting provider I managed to get it working. Problem has gone. However, if someone could explain why does OAuth is such rigourous on the precise date and time, I would be grateful.

stil
  • 4,876
  • 3
  • 35
  • 40