如何使用 Google 登录?
How to Login with Google?
我正在尝试在我的 Node.js 应用程序中不使用任何库的情况下实现 google oauth 2.0 登录。
我在 Google API 控制台上创建了一个应用程序,重定向 url 为 http://localhost:3000
。在登录期间,我的 response_type
是 code
,其中 returns 需要与 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: "
}
而不是用户令牌。
请帮我找出问题所在。
我也尝试在内容类型设置为 application/json
的 raw
中使用相同的负载进行 POSTMAN 查询,但它给了我同样的错误。
在通过 axios 进行交换调用时,您需要使用 params
代替 data
,修改后的块将像
params: {
code: code,
client_id: sso.clientId,
client_secret: sso.clientSecret,
redirect_uri: sso.redirect_uri,
grant_type: 'authorization_code',
}
希望对您有所帮助!
切勿在 GET 参数中包含诸如 clientSecret 之类的内容。这可能会导致严重的安全问题!
google 文档非常清楚 如何 发送 data
;
作为 POST 主体 - 一如既往地在 OAuth2 中:
https://developers.google.com/identity/protocols/OAuth2WebServer - 第 5 步,REST 代码示例
它们必须作为字符串发送,但在 POST 正文 / data
:
字符串是urlencoded参数,如
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=https://yourOauth2redirectUrl.example.com/code&
grant_type=authorization_code
我正在尝试在我的 Node.js 应用程序中不使用任何库的情况下实现 google oauth 2.0 登录。
我在 Google API 控制台上创建了一个应用程序,重定向 url 为 http://localhost:3000
。在登录期间,我的 response_type
是 code
,其中 returns 需要与 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: "
}
而不是用户令牌。
请帮我找出问题所在。
我也尝试在内容类型设置为 application/json
的 raw
中使用相同的负载进行 POSTMAN 查询,但它给了我同样的错误。
在通过 axios 进行交换调用时,您需要使用 params
代替 data
,修改后的块将像
params: {
code: code,
client_id: sso.clientId,
client_secret: sso.clientSecret,
redirect_uri: sso.redirect_uri,
grant_type: 'authorization_code',
}
希望对您有所帮助!
切勿在 GET 参数中包含诸如 clientSecret 之类的内容。这可能会导致严重的安全问题!
google 文档非常清楚 如何 发送 data
;
作为 POST 主体 - 一如既往地在 OAuth2 中: https://developers.google.com/identity/protocols/OAuth2WebServer - 第 5 步,REST 代码示例
它们必须作为字符串发送,但在 POST 正文 / data
:
字符串是urlencoded参数,如
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=https://yourOauth2redirectUrl.example.com/code&
grant_type=authorization_code