Sam*_*amB 5 node.js oauth-2.0 google-oauth2 passport-google-oauth
编辑:这是一个最小的可行项目
我正在尝试从服务器端流程的授权代码中获取Google的访问权限和刷新令牌.我在此处按照Google指南操作:https://developers.google.com/identity/sign-in/web/server-side-flow.
我使用的是护照和护照-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');
}
);
Run Code Online (Sandbox Code Playgroud)
这是此策略的护照配置.
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
}
));
Run Code Online (Sandbox Code Playgroud)
发出身份验证请求时,Google会响应以下错误:
{
"error" : "invalid_request",
"error_description" : "Invalid parameter value for redirect_uri: Missing scheme: /auth/google_auth_code/callback"
}
Run Code Online (Sandbox Code Playgroud)
以下是我在Google控制台中设置的凭据:
此时我不知道还能做什么.我还尝试将passport.use中的回调URL更改为绝对URL.我肯定会得到一个有效的身份验证代码(看起来像:) 4/Mb2pcOyhYhziROyFHKH5pnzvUldYbAmMop9SJKbBHXQ.如果我能提供更多相关信息,请与我们联系.
谢谢,
山姆
编辑
我注意到我的网址以正斜杠结尾.我解决了这个问题,但我没有更新截图.
如果我使用完整的URL(例如` http:// localhost:8080 // auth/google_auth_code/callback),我会收到以下错误:
{
"error" : "unauthorized_client"
}
Run Code Online (Sandbox Code Playgroud)
如果我使用完整的URL(例如` http:// localhost:8080/auth/google_auth_code/callback),我会收到以下错误:
{
"error" : "redirect_uri_mismatch"
}
Run Code Online (Sandbox Code Playgroud)
似乎是护照谷歌错误,您可能会在这里看到它: https: //github.com/jaredhanson/passport-google-oauth/issues/21
他们在这个问题中建议使用什么:https://github.com/sqrrrl/passport-google-plus
这就是开源的工作原理,你必须自己修复它或找到另一个包。
| 归档时间: |
|
| 查看次数: |
2361 次 |
| 最近记录: |