curl - 尝试刷新访问 token 时出现 Google Oauth2 invalid_grant 错误

标签 curl google-api google-oauth google-authentication

场景

我正在尝试使用提供的刷新 token 生成 Google OAuth2 访问 token 。刷新 token 是从客户端Javascript SDK获取的,使用以下答案中提到的离线访问方法:
https://stackoverflow.com/a/49842793/16341841

获得的刷新 token 被发送到后端服务器,后端服务器在需要时尝试使用此刷新 token 获取新的访问 token 。 API 端点以及用于相同目的的请求参数是:

curl --location --request POST 'https://oauth2.googleapis.com/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=<my_client_id> \
--data-urlencode 'client_secret=<my_client_secret>' \
--data-urlencode 'refresh_token=<refresh_token_sent_by_frontend>' \
--data-urlencode 'grant_type=refresh_token'

结果(错误)

我收到以下错误:

{
    "error": "invalid_grant",
    "error_description": "Bad Request"
}

我已经尝试过的

我已经尝试了以下链接中提到的所有适用(字面意义上的所有)解决方案以及答案中提到的内部链接:
invalid_grant trying to get oAuth token from google

注意事项

  1. 客户端 ID 和客户端密码在前端和后端完全相同。
  2. 每当我使用 OAuth2 API 通过 Postman 生成刷新 token 时,使用与上述相同的请求进行 token 刷新时,我根本不会遇到任何问题。所以我假设我在后端正确执行 token 刷新。

我还可能遗漏什么?非常感谢这里的一些帮助。

最佳答案

您构建的请求有误。它是一个 url 字符串。

curl \
--request POST \
--data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&grant_type=refresh_token' \
https://accounts.google.com/o/oauth2/token

Understanding Google OAuth 2.0 with curl

关于curl - 尝试刷新访问 token 时出现 Google Oauth2 invalid_grant 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68178100/

相关文章:

php - Crontab:PHP Wget 或 Curl

oauth - 为什么下载字幕会返回“需要登录”?

asp.net-web-api - WebApi 2 从 Google 身份验证 OpenID 更改 -> oAuth2 - 更改安全句柄

google-api - Google 登录无法使用

bash - 如何使用脚本创建一个到 Stratum 的套接字?

php - cURL无法完全模拟HTTP Post

c++ - 如何使用curl使用C++代码下载文件?

google-api - 无法在 Visual Studio 2010 Express 或更高版本上安装包 Google.Apis.Auth.Mvc

javascript - Uncaught ReferenceError : variableis not defined

python - 如何发布到 Google+?