An example google service account returns "OAuth2 token update error {" error ":" invalid_grant "}"

My goal is to make a simple request on Google Fusion Tablesbehalf of my users of web applications. To do this, I created a service account in the 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 many days, and most of the answers talk about updating the token. I made this update, but still the same mistakes!

Any idea to solve this problem? Thanks

+4
source share
3 answers

, , . , google.

, , : , 123456789-abcdebsbjf@developer.gserviceaccount.com

, , , Google, .

.

, .

+9

( 5 ).

, , -, , , Google, .

+11

I have the same error in the Google Analytical API, I solved it by adding mine Service accounts Email addressusing Google Analytics .

0
source

Source: https://habr.com/ru/post/1547150/


All Articles