google服务帐户示例返回"刷新OAuth2令牌{" Error ": " invalid_grant "}"


google service account example returns "Error refreshing the OAuth2 token { “error” : “invalid_grant” }"

我的目标是代表我的web应用程序用户对Google Fusion Tables进行最简单的查询。为此,我在google console上创建了一个服务帐户。下面是代码:

    // 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"
}'

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

有解决这个问题的办法吗?由于

在我的例子中,这是由于服务器的时间太远(大约5分钟)。

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

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

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

你要做的第二件事是与服务帐户的客户端id共享你在google融合表中查询的表。

在那之后,一切都会很顺利。

我在谷歌分析API中得到了同样的错误,我通过添加我的Service accounts Email address与谷歌分析帐户来解决它。