Google_Auth_Exception:获取OAuth2访问令牌时出错,消息:'invalid_request'

Nic*_*olò 5 google-api-php-client

我正在尝试使用最新版本的google-api-php-client获取访问令牌(请查看此https://github.com/google/google-api-php-client),然后我我正在关注此官方文档:https: //developers.google.com/api-client-library/php/guide/aaa_oauth2_web

唯一的区别是我想使用该服务Google_Service_Plus,而不是Google_Service_Urlshortener.

基本上我做这些步骤:

  • 我初始化Google客户端,$client指定客户端ID,客户端密钥,开发人员密钥,范围和重定向URI(指向我的localhost Ubuntu 12.04 - PHP 5.3.10).
  • $plus使用Google客户端初始化Google Plus服务$client.
  • 我使用将用户重定向到Auth URL $client->createAuthUrl().
  • 用户可以看到同意页面并接受.
  • 我获得了$_GET['code']用于令牌交换的授权代码.
  • 然后,在回调页面中,我尝试做$client->authenticate($_GET['code']);但谷歌服务器返回HTTP 400,我得到了一个Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_request'.

这里的流动打破了.
怎么了?

任何帮助将不胜感激.

提前谢谢,
Niccolò.

Gav*_*mer 8

我创建了一个oauth2callback.php文件,并遇到了这个问题.当我正确设置Google_Client时,问题就消失了.起初我只设置了clientId和secret.一旦我设置了redirectUri,我的代码就开始工作了.我不认为我需要设置redirectUri,因为我知道重定向已经发生(我在重定向页面上).我认为这个答案可能会帮助其他人遇到类似的问题.

  • 这正是我的问题.由于我使用的是`server-side-flow`,因此redirectUri必须设置为"postmessage`".那么,瞧! (3认同)