【问题标题】:Get google OAuth 2.0 refresh token using PassPortJS and Node.jS使用 PassPortJS 和 Node.jS 获取 google OAuth 2.0 刷新令牌
【发布时间】:2017-09-05 22:27:02
【问题描述】:

我无法使用 Passport JS 从 Google 获取刷新令牌。我一直在删除我的帐户权限中的访问权限,以确保它是第一次登录。我不断收到令牌的“未定义”。这是我目前拥有的代码。

Passport.use(new GoogleStrategy({
    clientID:     "xxxxx.apps.googleusercontent.com",
    clientSecret: "xxxxx",
    callbackURL: `https://localhost:${port}/auth/google/callback`,
    accessType: 'offline',
    passReqToCallback: true
  },
  function(req,accessToken, refreshToken, profile, done) {

    var tokens = {
      access_token: accessToken,
      refresh_token: refreshToken
    }
    console.log(tokens)
    oauth2Client.setCredentials(tokens);


    done(err = false, user = true)


}));

如果有人有在他们的网络应用程序中使用 OAuth 的经验,可以提供帮助,我们将不胜感激

【问题讨论】:

    标签: node.js oauth-2.0 passport.js


    【解决方案1】:

    答案是在内部路由期间请求它并每次创建同意提示。

    app.get('/auth/google', passport.authenticate('google',{accessType: 'offline', prompt: 'consent', scope: ['openid profile email https://www.googleapis.com/auth/drive']}))
    

    【讨论】:

      【解决方案2】:

      要在 NodeJS 中使用 passportJS 获取刷新令牌,您只需在路由器部分添加另外 2 个访问字段 ( accessType: 'offline', prompt: 'consent' )

      ex.router.get('/auth/google', passport.authenticate('google', {
          scope: ['profile'],
          accessType: 'offline',
          prompt: 'consent'       
      }));
      

      之后,您将收到一个刷新令牌。

      注意:如果您的应用程序未通过 Google 验证,则会向您发出警告。 只需点击下面的 advance 按钮,您就可以开始了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-05
        • 2013-10-27
        • 1970-01-01
        • 2015-11-26
        • 2021-03-20
        • 1970-01-01
        • 2015-04-01
        • 2014-07-15
        相关资源
        最近更新 更多