我正在尝试在 Node.js 应用程序中不使用任何库的情况下实现 google oauth 2.0 登录。
我在 Google API 控制台上创建了一个应用程序,重定向 URL 为 http://localhost:3000
。登录期间,我的 response_type
为 code
,它返回一次性使用代码,需要与 token_endpoint 进行交换,如 here 中所述。 。
交换是在我的 Node.js 服务器上使用以下代码片段完成的。
axios({
url: 'https://www.googleapis.com/oauth2/v4/token',
method: 'post',
data: {
code: code,
client_id: sso.clientId,
client_secret: sso.clientSecret,
redirect_uri: sso.redirect_uri,
grant_type: 'authorization_code',
}
})
.then((response) => {
console.log(response.data);
})
.catch(function(err) {
console.log(err.response.data);
});
{
"error": "unsupported_grant_type",
"error_description": "Invalid grant_type: "
}
而不是用户 token 。
请帮我找出问题所在。
我尝试在 raw
中使用相同的负载进行 POSTMAN 查询,内容类型设置为 application/json
,但它给了我同样的错误。
最佳答案
在通过 axios 进行交换调用时,您需要使用 params
代替您的 data
,修改后的 block 将类似于
params: {
code: code,
client_id: sso.clientId,
client_secret: sso.clientSecret,
redirect_uri: sso.redirect_uri,
grant_type: 'authorization_code',
}
希望这有帮助!
关于javascript - 如何使用谷歌登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129835/