【问题标题】:google service account example returns "Error refreshing the OAuth2 token { “error” : “invalid_grant” }"google 服务帐户示例返回“刷新 OAuth2 令牌时出错 { “error” : “invalid_grant” }”
【发布时间】:2014-07-04 10:18:28
【问题描述】:

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

    // 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);

运行这段代码后,我得到了这个错误:

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

我用谷歌搜索了几天,大多数答案都在谈论刷新令牌。我进行了这次刷新,但仍然出现同样的错误!

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

【问题讨论】:

    标签: php symfony google-fusion-tables google-api-php-client service-accounts


    【解决方案1】:

    在我的情况下,这是由于服务器的时间太远(大约 5 分钟)造成的。

    虽然您的问题已经解决,但也许这对将来登陆这里的人有任何帮助,因为谷歌返回的错误是相同的。

    【讨论】:

    • 如何检查我的机器时间是否不同。请帮忙
    • @BhupenderKeswani 保持服务器时间同步的最简单方法是使用NTP。要检查服务器上的当前时间,请使用date 命令。
    • 哇,谢谢比约恩的小费。我尝试了许多其他的可能性。 :)
    【解决方案2】:

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

    我提出的代码只有一个错误:client id is like an email类似于123456789-abcdebsbjf@developer.gserviceaccount.com

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

    之后,事情就完美了。

    我希望这对其他人有帮助。

    【讨论】:

    • 我正在尝试做这样的事情,stackoverflow.com/questions/21470405/…,它给了我同样的错误!
    • 您应该先创建$client 并设置其凭据,然后使用SpreadsheetService
    • amine jallouli 你能详细说明一下吗,作为我刚刚提出的这个问题的答案:stackoverflow.com/questions/26965299/…
    • 创建$client 是什么意思?我应该调用什么方法,参数是什么?
    • 在我的问题中,我给出了如何创建 de \Google_Client(),然后是 \Google_Auth_AssertionCredentials,最后是 \Google_Service_Fusiontables。您必须遵循这些步骤。对于您的情况,您将使用 `Google\Spreadsheet\SpreadsheetService`。
    【解决方案3】:

    我在 google analytic API 中遇到了同样的错误,我已经通过添加我的 Service accounts Email addressgoogle analytic 帐户来解决它。

    【讨论】:

      猜你喜欢
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      相关资源
      最近更新 更多