根据身份验证代码流程,我正在尝试从Google获取访问令牌。
由于它在我的后端不起作用,我试图卷曲/邮差,但这也不起作用。
curl -d "&code=4/IkYUPa4FNw_-o6bd4v5dLqVx4ssGfyPJRBQFzy9aNQ&client_id=807080008535-9ji0g54v0lt9b6ukn19rp807k3j2t0uj.apps.googleusercontent.com&client_secret=SOMESECRET&redirect_uri=com.fitisfit.app:/googlefit&grant_type=authorization_code" https://www.googleapis.com/oauth2/v4/token
这是我得到的错误:
{
"error": "invalid_grant",
"error_description": "Bad Request"
}
如果我连续多次卷曲(3或4),我会收到错误消息,说明代码已经兑换。因此,Google网站上可能会发生一些事情。
我做了一些研究,但没有任何建议有帮助(拼写,检查服务器时间,将access_type=offline
添加到第一个OAuth步骤网址等)。
更新: 我刚刚发现了这个问题。我必须设置curl参数,如:
curl \
-d code=4/LjNMkGqqQvJAB96Z6F-U52u4kqo5RHkqLp0LJYZ-zAw \
-d client_id=807080008535-9ji0g54v0lt9b6ukn19rp807k3j2t0uj.apps.googleusercontent.com \
-d client_secret=SOMESECRET \
-d redirect_uri=http://localhost \
-d grant_type=authorization_code https://www.googleapis.com/oauth2/v4/token
答案 0 :(得分:1)
在生成代码时,请不要写 access_type ='offline'
使用邮递员生成代码: API:https://accounts.google.com/o/oauth2/v2/auth 参数:
使用此方法,您将获得代码,然后使用与curl相同的参数调用https://www.googleapis.com/oauth2/v4/token API。
答案 1 :(得分:1)
我解决这个问题的方法是对 redirect_uri
进行编码。
我在邮递员中使用了以下内容:
redirect_uri = https%3A//localhost.com
为了避免出现问题,请复制谷歌提供的示例并将参数更改为我自己的。
这是代码:
https://accounts.google.com/o/oauth2/v2/auth?
scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly&
access_type=offline&
include_granted_scopes=true&
response_type=code&
state=state_parameter_passthrough_value&
redirect_uri=https%3A//oauth2.example.com/code&
client_id=client_id
这对于access_token
:
POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=https%3A//oauth2.example.com/code&
grant_type=authorization_code
希望它对你有用!