【问题标题】:Passport Get Refresh Token OAuthPassport 获取刷新令牌 OAuth
【发布时间】:2021-01-18 10:04:17
【问题描述】:

如何使用 Passport 在 OAuth 策略中获取刷新令牌?我可以获取访问令牌,但也需要获取刷新令牌。

从谷歌文档中,发现我们需要在请求中添加access_type=offline。 如何使用护照实现它。

//STRATEGY
passport.use(
  new GoogleStrategy(
    {
      clientID: googleKeys.clientID,
      clientSecret: googleKeys.clientSecret,
      callbackURL: urls.cb,
    },
    (accessToken: any, refreshToken: any, profile: any, cb: any) => {

      //refreshToken is undefined
      cb(null, { email: profile._json.email });

    }
  )
);

router.get(
  "/auth/google",
  passport.authenticate("google", {
    scope: [
      "https://www.googleapis.com/auth/userinfo.profile",
      "https://www.googleapis.com/auth/userinfo.email",
    ],
  })
);

//REDIRECT URI
router.get(
  "/auth/google/callback",
  passport.authenticate("google", { failureRedirect: "/", session: false }),
  (req: Request, res: Response) => {
    res.send("Success");
  }
);

【问题讨论】:

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


    【解决方案1】:

    使用passport.authenticate时需要将访问类型作为参数传递。

    router.get(
      "/auth/google",
      passport.authenticate("google", {
        scope: [
          "https://www.googleapis.com/auth/userinfo.profile",
          "https://www.googleapis.com/auth/userinfo.email",
        ],
        accessType: 'offline',
        prompt: 'consent',
      })
    );
    

    参考:https://github.com/jaredhanson/passport-google-oauth2/issues/4#issuecomment-344460414

    【讨论】:

    • 在打字稿中给出错误,但如果打字稿被忽略,则可以工作。
    猜你喜欢
    • 2019-05-02
    • 2015-11-24
    • 2020-04-07
    • 2015-11-26
    • 2020-05-05
    • 2017-04-10
    • 1970-01-01
    • 2013-11-05
    • 2014-07-15
    相关资源
    最近更新 更多