场景
我正在尝试使用提供的刷新 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
注意事项
- 客户端 ID 和客户端密码在前端和后端完全相同。
- 每当我使用 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
关于curl - 尝试刷新访问 token 时出现 Google Oauth2 invalid_grant 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68178100/