【发布时间】:2017-08-27 00:51:56
【问题描述】:
编辑:here is a minimal viable project
我正在尝试从服务器端流程的授权代码中从 Google 获取访问和刷新令牌。我在这里遵循了 Google 的指南:https://developers.google.com/identity/sign-in/web/server-side-flow。
我正在使用护照和passport-google-authcode。
以下是节点应用的路由:
router.get('/auth/google_auth_code',
passport.authenticate('google_authcode', {
scope:
[
'https://www.googleapis.com/auth/calendar',
'profile',
'https://www.googleapis.com/auth/userinfo.email'
]
}),
function () {
res.end();
})
router.get('/auth/google_auth_code/callback',
passport.authenticate('google_authcode', {
failureRedirect: '/error'
}),
function (req, res) {
// do something with req.user
res.send('hello');
}
);
这是此策略的护照配置。
passport.use('google_authcode', new GoogleAuthCodeStrategy({
clientID: 'my client id',
clientSecret: 'my secret',
callbackURL: '/auth/google_auth_code/callback',
// passReqToCallback: true
},
function (accessToken, refreshToken, rawResponse, profile, done) {
// unable to get here
}
));
发出身份验证请求时,Google 会返回以下错误:
{
"error" : "invalid_request",
"error_description" : "Invalid parameter value for redirect_uri: Missing scheme: /auth/google_auth_code/callback"
}
这是我在 Google 控制台中的凭据设置:
此时我不知道还能做什么。我还尝试将 passport.use 中的回调 URL 更改为绝对 URL。我肯定会取回一个有效的身份验证代码(它看起来像:4/Mb2pcOyhYhziROyFHKH5pnzvUldYbAmMop9SJKbBHXQ)。如果我可以提供更多相关信息,请告诉我。
谢谢,
山姆
编辑
我注意到上面的 URL 以正斜杠结尾。我解决了这个问题,但我还没有更新屏幕截图。
如果我使用完整的网址(例如 `http://localhost:8080//auth/google_auth_code/callback),我会收到以下错误:
{
"error" : "unauthorized_client"
}
如果我使用完整的网址(例如 `http://localhost:8080/auth/google_auth_code/callback),我会收到以下错误:
{
"error" : "redirect_uri_mismatch"
}
【问题讨论】:
-
http://localhost:8080//auth/google_auth_code/callback为什么端口号后面有 2 个斜线? -
@SagarV 当我在帖子中添加信息时,这一定是一个错字。我只是加倍检查并使用完整(正确)的 URL,我现在收到
redirect_uri_mismatch错误。这让我觉得你不应该指定主机。 -
在授权
JavaScript origins中,而不是http://localhost尝试http://localhost/auth并检查它是否工作 @SamB -
控制台不允许我添加 /auth 因为
Origin URIs must not contain a path or end with "/": http://localhost:8080/auth -
我读过这个。但要说清楚,我要问。您是否使用
localhost而不是这个callbackURL: '/auth/google_auth_code/callback'来确定网址?
标签: node.js oauth-2.0 google-oauth passport-google-oauth