Google OAuth invalid_grant /错误请求 - 说明中的解决方案

问题描述 投票:5回答:1

根据身份验证代码流程,我正在尝试从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 \
  -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
curl oauth google-oauth2
1个回答
2
投票

在生成代码时,请不要写access_type ='offline'

使用邮递员生成代码:API:https://accounts.google.com/o/oauth2/v2/auth参数:

  1. client_id ='您的客户ID'
  2. response_type ='code'
  3. scope ='https://www.googleapis.com/auth/blogger'
  4. redirect_uri ='你的网址就像我的:https://www.trybotics.com'

使用此代码,您将获得代码,然后使用与curl相同的参数调用https://www.googleapis.com/oauth2/v4/token API。

© www.soinside.com 2019 - 2024. All rights reserved.