google服务帐户示例返回“刷新OAuth2令牌时发生错误{"error":"invalid_grant"}“
我的目标是代表Web应用程序用户在Google Fusion Tables
上进行最简单的查询.为此,我在Google控制台上创建了一个服务帐户.这是代码:
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"
}'
我用Google搜索了好几天,大多数答案都在谈论刷新令牌.我做了刷新,但仍然是相同的错误!
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
我知道许多人都面临着与我相同的问题.因此,这是在Google上搜索几天后的解决方案.
I know that many people are facing the same problem like mine. So, this is the solution after days of search on google.
我提出的代码只有一个错误:客户端ID类似于类似于123456789-abcdebsbjf@developer.gserviceaccount.com
The code I proposed has only one error: the client id is like an email similar to 123456789-abcdebsbjf@developer.gserviceaccount.com
您要做的第二件事是与服务帐户的客户ID共享您在google融合表中查询的表.
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.