可以';t使用php表单在googleoauth上交换令牌的授权代码


Can't exchange authorization code for token on google oauth using php form

我可以很容易地获得授权代码,但在尝试将其交换为访问令牌时,我会不断出错。

    <FORM action="https://accounts.google.com/o/oauth2/auth" method="post">
<input type = "hidden" name = "code" value = "4%2FXR0s9YvmROnHOpNUuiSlfxHE7xj-">    
<input type = "hidden" name = "redirect_uri" value = "http%3A%2F%2Fwww.m1448analytics.com%2Foauth2%2Frecv.php"> 
<input type = "hidden" name = "client_id" value = "502294831256-84q1ar05j14knsh5h2msvofu68e7rpaj.apps.googleusercontent.com">   
<input type = "hidden" name = "scope" value = "">   
<input type = "hidden" name = "client_secret" value = "hiding it">  
<input type = "hidden" name = "grant_type" value = "authorization_code">    
<INPUT type="submit" value="Send"> 
</FORM>

谷歌表示:错误:invalid_request缺少必需的参数:response_type了解更多信息请求详细信息

您请求的URL是:

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

这是用于获取身份验证代码的URL。现在,如果你想获得一个令牌,那么URL应该是:

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

更改此项,您的代码就会正常工作。

您可能缺少response_type=code

response_type参数确定Google OAuth 2.0端点是否返回授权代码。如果您正在web服务器上编写应用程序,则应该使用code

https://developers.google.com/accounts/docs/OAuth2WebServer