谷歌 OAUTH2 交换访问令牌的授权代码“无效请求”


google OAUTH2 exchange authorization code for acces token "invalid request"

我从服务器上的此页面获取谷歌 api 授权代码

https://github.com/google/google-api-php-client/blob/master/examples/user-example.php

我的主机上的同一页面进行测试

http://mawk3y.net/google/google-api-php-client/examples/user-example.php

调整客户端 ID、机密和重定向 URI 后。

$data =file_get_contents('https://accounts.google.com/o/oauth2/auth?code='.$code.'&client_secret={secret}&redirect_uri={my web page}&grant_type=authorization_code');
print_r($data);

但是我收到一个错误,所以我在从该页面获取授权代码后尝试将完整的 URL 粘贴到浏览器地址栏,如下所示(与我从此页面获得的身份验证代码相同 https://developers.google.com/oauthplayground/)

https://accounts.google.com/o/oauth2/token?code={authorization code}&redirect_uri=mywebpage.php&client_id={my client id}&client_secret={secret code}&grant_type=authorization_code

但结果是

{
"error" : "invalid_request"
}

如何解决此问题并将授权代码交换为访问令牌

您要将 GET 请求中的参数发送到授权终端节点 (https://accounts.google.com/o/oauth2/auth),但您必须在 POST 请求中将它们发送到令牌终端节点 (https://accounts.google.com/o/oauth2/token)。