doujiao1981 2014-08-10 00:09
浏览 88
已采纳

Google PHP客户端不会验证代码,invalid_request

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']);

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 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.

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):

{"code":"4\/58wTCTNiQNIdRfb8DgQBYk518URV.Elrb71kFKHAYEnp6UAPFm0HWWGd6jwI","grant_type":"authorization_code","redirect_uri":"","client_id":"807284957448-3katmu7oqd1277ql9eo258dadkbkqruq.apps.googleusercontent.com","client_secret":"HIDDEN"}

What could I possibly be doing wrong?!

  • 写回答

2条回答 默认 最新

  • doulutian4843 2014-08-10 19:05
    关注

    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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程