7

编辑这是一个最小的可行项目

我正在尝试从服务器端流程的授权代码中从 Google 获取访问和刷新令牌。我在这里遵循了 Google 的指南:https ://developers.google.com/identity/sign-in/web/server-side-flow 。

我正在使用 passport 和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"
}
4

1 回答 1

1

似乎是护照谷歌错误,您可能会在这里看到: https ://github.com/jaredhanson/passport-google-oauth/issues/21

他们在这个问题上建议使用什么:https ://github.com/sqrrrl/passport-google-plus

它是开源的工作方式,你必须自己修复它或找到另一个包。

于 2017-04-10T18:51:03.747 回答