无法使用passportjs检索Twitter的电子邮件ID

时间:2018-03-16 04:51:24

标签: email twitter oauth passport.js

const TwitterStrategy = require('passport-twitter').Strategy;

module.exports = (passport, User) => {
  passport.use(new TwitterStrategy({
    consumerKey: 'paLCHei1bz8uG5mzNK3ZmvWy7',
    consumerSecret: 'djwTrPntW6T2hm3EJ6eIvUrqObMSymgKn6B1foMQyNeypjtbIK',
    callbackURL: 'http://localhost:3000/auth/twitter/callback',
    passReqToCallback: true
  }, (req, accessToken, tokenSecret, profile, done) => {
    User.findOne({ 'twitter.id': profile.id }, (err, x) => {
      if (x) return done(null, x);
      var user = {
        image: profile._json.profile_image_url,
        displayName: profile.displayName,
        email: profile.emails[0].value,
        twitter: {
          id: profile.id,
          email: profile.emails[0].value
        }
      };
      User.create(user, (err, x) => done(null, x));
    });
  }));
};

1 个答案:

答案 0 :(得分:0)

您需要在Twitter上的应用权限中选中“向用户请求电子邮件地址”复选框。您可以阅读更多相关信息here