google OAUTH2 交换访问令牌的授权代码 "invalid request"

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

我正在从我服务器上的这个页面获得 google 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 请求中的参数发送到 authorization 端点 (https://accounts.google.com/o/oauth2/auth), but you must send them in a POST request to the token endpoint (https://accounts.google.com/o/oauth2/token)。