0

我正在编写代码以添加使用 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('/')
  });
4

1 回答 1

0

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

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

于 2020-07-28T21:17:59.833 回答