使用 Google oAuth2 服务器端身份验证时出现 400 错误代码

400 error code when using Google oAuth2 server-side authentication

在我基于 PHP 的应用程序中,多年来我一直在使用 Google oAuth 2 服务器端流程,它一直运行完美,直到最近。我一直在阅读任何可能的重大 API 更改,但找不到任何内容。大多数有类似问题的问题都已经有好几年了,所以我要问一个新问题。特别奇怪的是,这在我这边没有任何改变就停止了。

以下详细信息来自我的开发环境,但我在生产环境中得到了类似的结果。这是我用来获取权限的 URL(不确定正确的术语是什么):

https://accounts.google.com/o/oauth2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&state=%2Fprofile
&redirect_uri=http%3A%2F%2Fwww.jungledragon.org%2Fapps%2Fjd4%2Fsignin%2Fgoogle%2Fcallback
&response_type=code
&client_id=652476484487.apps.googleusercontent.com
&approval_prompt=auto

这似乎工作正常。如果尚未提供,用户确实会看到 Google 屏幕以授予访问权限。如果用户随后批准收益,他们将在回调 URL.

的情况下被重定向回我的应用程序

获得许可后,下一个目标是获取一些用户数据。为此,使用以下代码:

        // get values from the callback URL
        parse_str($_SERVER['QUERY_STRING'],$_GET);
        $code = $_GET['code'];
        $error = $_GET['error'];
        $state = $_GET['state'];

        // an error reason is returned, something went wrong
        if ($error_reason) { return false; }

        // our app is recognized, get access token by doing a post to the Google oAuth service
        $url = 'https://accounts.google.com//o/oauth2/token';
        $data = 
        "code=" . urlencode($code) . 
        "&client_id=" . $this->CI->config->item('pd_oauth_google_clientid') . 
        "&client_secret=" . $this->CI->config->item('pd_oauth_google_clientsecret') . 
        "&redirect_uri=" . urlencode($this->CI->config->item('pd_oauth_google_callbackurl')) . 
        "&grant_type=authorization_code";

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);

        // check the result. anything but a 200 return code is an error
        $info = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        if ($info!=200) { return false; }

如您所见,CURL POST 请求是使用多个参数创建的。 None 的参数值已更改,这已工作多年,但由于未知原因现在停止工作。

具体问题是 post 的响应是 Google Error 404 (Not Found)!! 页面。这没有给我任何关于可能出错的有意义的信息。

非常感谢您的帮助,因为这个问题阻止了我的生产站点上通过 Google 身份验证登录的所有用户。

https://accounts.google.com//o/oauth2/token

将导致 404

https://accounts.google.com/o/oauth2/token

将导致

{
  "error" : "invalid_request"
}

不知道你的代码为什么在去年有效你有一个额外的 / 在那里