Google服务帐户示例返回"刷新OAuth2令牌时出错{"错误":"invalid_grant"}"

Ami*_*uli 4 php symfony google-fusion-tables google-api-php-client service-accounts

我的目标是Google Fusion Tables代表我的网络应用用户进行最简单的查询.为此,我在谷歌控制台上创建了一个服务帐户.这是代码:

    // 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);
Run Code Online (Sandbox Code Playgroud)

运行此代码后,我收到此错误:

Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
}'
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索了几天,大多数答案都在谈论刷新令牌.我做了这个刷新,但仍然是同样的错误!

有什么想法解决这个问题吗?谢谢

Bjo*_*orn 11

在我的情况下,它是由服务器的时间太远(大约5分钟)引起的.

虽然您的问题已经解决,但对于将来登陆此处的人来说,这可能会有任何帮助,因为Google返回的错误是相同的.


Ami*_*uli 9

我知道很多人都像我一样面临同样的问题.所以,这是谷歌搜索几天后的解决方案.

我提出的代码只有一个错误:客户端ID就像一封类似的电子邮件 123456789-abcdebsbjf@developer.gserviceaccount.com

您要做的第二件事是使用服务帐户的客户端ID共享您在google fusion表中查询的表.

在那之后,事情将完美地运作.

我希望这会有助于其他人.