首页 > 解决方案 > Google Auth Exchange 代码、Refresh Token 和 Access Token

问题描述

我正在尝试refereh_token, access_token使用 Google Auth Exchange 代码,查看文档但它不起作用,我正在使用 react-google-login 进行身份验证并获得code响应,并且我正在传递code https://accounts。 google.com/o/oauth2/token但它给出错误

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

我尝试了很多方法

code=4%2FKxoYTS-jeq5-d6Lv7YvSz9ZrK0pJ_5lZsMExzNC1M0o&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=407408718192.apps.googleusercontent.com&client_secret=************&scope=&grant_type=authorization_code

前端是 reactjs,后端是 nodejs..

标签: node.jsreactjsgoogle-authenticationrefresh-token

解决方案


要使用referesh令牌获取访问令牌,您必须调用一个api,这将为您提供一个新的访问令牌调用POST APi https://www.googleapis.com/oauth2/v4/token

将正文作为 x-www-form-urlencoded 传递,即

标头作为 Content-Type : application/x-www-form-urlencoded

身体

client_secret:'xxxxxxxxxxxxdxxxxxxSSd'<YourClientSecret>

授予类型:刷新令牌'

refresh_token : '1/DDPkddk514451xxxxxxxx'<YourRefreshToken>

client_id : 'xxxxxxxxxxxxxxx' <YourClientID>

如需更多参考,您可以阅读此https://developers.google.com/identity/protocols/OAuth2WebServer


推荐阅读