【问题标题】:How to Get user Email Address using OAuth in Node js如何在 Node js 中使用 OAuth 获取用户电子邮件地址
【发布时间】:2020-07-29 05:17:59
【问题描述】:

我正在编写一个代码来添加使用 Google 登录的功能。我已经编写了代码,但是当用户使用 google 登录时,它只给我 id、姓名、全名等。它不提供用户电子邮件地址。谁能帮我解决这个问题?以下是我的代码

passport.use(new GoogleStrategy({
    clientID: CLIENT_ID,
    clientSecret: CLIENT_SECRET,
    callbackURL: "http://localhost:8000/auth/google/notepad"
  },
  function(accessToken, refreshToken, profile, cb) {
    console.log(profile);
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));


router.get('/auth/google', passport.authenticate('google',{scope: ['profile']}));
router.get('/auth/google/notepad', 
  passport.authenticate('google', { failureRedirect: '/' }),
  async function(req, res) {
    const token = await req.user.generateAuthToken();
    res.cookie('authToken', token);
    res.redirect('/')
  });

【问题讨论】:

    标签: node.js passport-google-oauth


    【解决方案1】:

    您缺少email 范围。这是他们个人资料的单独范围。

    如果您想了解更多信息,也请参阅文档:https://developers.google.com/identity/protocols/oauth2/openid-connect#scope-param

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-08
      • 2021-06-02
      • 2011-05-06
      • 2013-03-29
      • 2016-10-12
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      相关资源
      最近更新 更多