Google PHP客户端不会验证代码invalid_request [英] Google PHP client won't authenticate code, invalid_request
问题描述
我正在使用谷歌PHP客户端4ae272683e18888362e1f935b813e345b99e23b8从8月9日拉到github。我觉得我的代码太简单了,不会出错。
I'm using Google PHP Client 4ae272683e18888362e1f935b813e345b99e23b8 pulled Aug 9 from github. I feel like my code is too simple to be wrong.
require_once ('Google/Client.php');
$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
$client->authenticate($_POST['code']);
我得到这个 invalid_request
错误: p>
I get this invalid_request
error:
Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_request'' in /Users/dfabulich/test/Google/Auth/OAuth2.php:125
Stack trace:
#0 /Users/dfabulich/test/Google/Client.php(135): Google_Auth_OAuth2->authenticate('4/58wTCTNiQNIdR...')
#1 /Users/dfabulich/test/google-login.php(24): Google_Client->authenticate('4/58wTCTNiQNIdR...')
#2 {main}
client_secret.json
是我下载的确切文件来自Google API开发人员控制台。该文件绝对存在,因为如果我使用错误的文件名,我会得到一个非常明显的错误,无效的客户端秘密JSON文件。
client_secret.json
is the exact file I downloaded from the Google API Developers Console. The file definitely exists, because if I use the wrong file name, I get a very clear error, "Invalid client secret JSON file." I've visually inspected the file and it looks fine.
我编辑了Google / Auth / OAuth2.php以将帖子正文记录为json;它看起来像这样(但我已经隐藏在下面的客户端密钥):
I edited Google/Auth/OAuth2.php to log the post body as json; it looks like this (but I've HIDDEN the client secret below):
{ 代码: 4\ / 58wTCTNiQNIdRfb8DgQBYk518URV.Elrb71kFKHAYEnp6UAPFm0HWWGd6jwI, grant_type: authorization_code, REDIRECT_URI: , CLIENT_ID: 807284957448-3katmu7oqd1277ql9eo258dadkbkqruq.apps.googleusercontent.com, client_secret: 隐藏}
{"code":"4\/58wTCTNiQNIdRfb8DgQBYk518URV.Elrb71kFKHAYEnp6UAPFm0HWWGd6jwI","grant_type":"authorization_code","redirect_uri":"","client_id":"807284957448-3katmu7oqd1277ql9eo258dadkbkqruq.apps.googleusercontent.com","client_secret":"HIDDEN"}
我可能会做错什么?!
推荐答案
我使用的是Google+登录API,因此我还需要添加一个 setRedirectUri(postmessage)
行。此代码有效。
I was using the Google+ Sign-in API, so I also needed to add a setRedirectUri("postmessage")
line. This code works.
require_once ('Google/Client.php');
$client = new Google_Client();
$client->setRedirectUri('postmessage');
$client->setAuthConfigFile('client_secret.json');
$client->authenticate($_POST['code']);
这篇关于Google PHP客户端不会验证代码invalid_request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!