4

My goal is to make the simplest query on Google Fusion Tables on behalf of my web app users. For that, I created a service account on google console. Here is the code:

    // Creating a google client
    $client = new \Google_Client();


    // setting the service acount credentials
    $serviceAccountName = 'XXXXX.apps.googleusercontent.com';
    $scopes= array(
                'https://www.googleapis.com/auth/fusiontables',
                'https://www.googleapis.com/auth/fusiontables.readonly',
                );
    $privateKey=file_get_contents('/path/to/privatekey.p12');
    $privateKeyPassword='notasecret'; // the default one when generated in the console

    $credential = new \Google_Auth_AssertionCredentials($serviceAccountName,
            $scopes, $privateKey, $privateKeyPassword);

    // setting assertion credentials
    $client->setAssertionCredentials($credential);

    $service = new \Google_Service_Fusiontables($client);
    $sql = 'select name from XXXXXXXX'; 
    $result = $service->query->sql($sql);

After running this code, I got this error:

Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
}'

I googled that for days and most of answers are talking about refreshing the token. I made this refresh but still the same errors!

Any idea for solving this problem? Thanks

Amine Jallouli
  • 3,919
  • 8
  • 36
  • 73

3 Answers3

11

In my case it was caused by the server's time being too far off (about 5 minutes).

Although your issue is already solved, maybe this is of any help for someone who lands here in the future as the error returned by Google is the same.

Bjorn
  • 5,272
  • 1
  • 24
  • 35
  • How to check if my machine time is different. Please help – Bhupender Keswani Oct 09 '15 at 11:57
  • 1
    @BhupenderKeswani the easiest way to keep your server's time in sync is to use [NTP](https://help.ubuntu.com/lts/serverguide/NTP.html). To check the current time on the server, use the `date` command. – Bjorn Oct 10 '15 at 09:49
  • Wow, thanks for the tip Bjorn. I had tried many other possibilities. :) – Mino Nov 26 '15 at 05:34
9

I know that many people are facing the same problem like mine. So, this is the solution after days of search on google.

The code I proposed has only one error: the client id is like an email similar to 123456789-abcdebsbjf@developer.gserviceaccount.com

The second thing you have to do is to share the table you are quering in google fusion tables with the client id of the service account.

After that, thing will work perfectly.

I hope this would help other.

Amine Jallouli
  • 3,919
  • 8
  • 36
  • 73
  • I am trying to do something like this, http://stackoverflow.com/questions/21470405/google-api-how-to-connect-to-receive-values-from-spreadsheet , and it is giving me the same error! – Mike Warren Nov 17 '14 at 01:47
  • You should create the `$client` and set its credentials first, then use `SpreadsheetService` – Amine Jallouli Nov 17 '14 at 02:20
  • amine jallouli could you elaborate on this, as an answer to this question I just posed: http://stackoverflow.com/questions/26965299/trying-to-get-google-accesstoken ? – Mike Warren Nov 17 '14 at 04:01
  • What do you mean by creating the `$client`? What methods should I invoke, and what are the parameters? – Mike Warren Nov 17 '14 at 05:19
  • In my question, I gave how to create de `\Google_Client()`, then `\Google_Auth_AssertionCredentials` and finally the `\Google_Service_Fusiontables`. You have to follow these steps. For your case, you will use ` Google\Spreadsheet\SpreadsheetService`. – Amine Jallouli Nov 17 '14 at 13:12
  • I was trying to use my account email vs the service account email. Makes a world of difference! – Jared Eitnier Apr 11 '15 at 13:47
0

I got same error in google analytic API, I have solved it by adding my Service accounts Email address with google analytic account.

Nishad Up
  • 3,457
  • 1
  • 28
  • 32