passport-facebook - 无法获取about_me和电子邮件配置文件字段

时间:2013-11-29 18:08:46

标签: node.js facebook mongoose passport.js

我正在尝试使用passport-facebook为我的应用程序建立登录系统。 一切顺利,除了从请求中得到未定义的2个字段。

我将发布我的整个代码用于登录程序,因为我在这里没有看到很多关于它的信息,即使在这个问题上有很多问题。

这是app.js中的配置

var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;

passport.serializeUser(function(user, done) {
  done(null, user.facebookId);
});
passport.deserializeUser(function(id, done) {
  routes.findUserById(id, function(err, user) {
    done(err, user);
  });
});

passport.use(new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: FACEBOOK_CALLBACK_URL,
    profileFields: ['id', 'displayName', 'link', 'about_me', 'photos', 'email']
  },
  routes.handleLogin
));

使用护照初始化和会话

app.use(passport.initialize());
app.use(passport.session());

实际请求处理,请注意我使用的是正确的范围

app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['user_about_me', 'email'] }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/error' }));

这是我在路由器中的用户创建功能

exports.handleLogin = function(accessToken, refreshToken, profile, done) {
  db.userCatalog.findOne({ facebookId: profile.id }, function(err, existingUser) {
    if (err) {
      return done(err);
    }
    else {
      if(existingUser) {
        console.log('User: ' + existingUser.name + ' found and logged in!');
        done(null, existingUser);
        } 
      else {
        new db.userCatalog({
        name: profile.displayName,
        facebookId: profile.id,
        link: profile.link,
        picture: profile.photos[0].value,
        bio: profile.about_me,
        email: profile.email
        }).save(function (err, data) {
          if (err) {
            return done(err);
          }
          else {
            console.log('New user: ' + data + ' created and logged in!');
            done(null, data);
          }
        });
      }
    }
  });
};

以及完成登录过程后创建新用户时的结果: enter image description here

我确信这是一个新手的错误,但我自己无法理解......

2 个答案:

答案 0 :(得分:38)

Facebook返回一些默认属性。如果您想要访问有关客户端配置文件的更多详细信息,则必须在FacebookStrategy下声明它:

passport.use(new FacebookStrategy({

        clientID: "...",
        clientSecret: "...",
        callbackURL: "...",
        profileFields: ['id', '...', '...', 'photos', 'emails']

      }, ...

因此,一旦您宣布要从Facebook收到的属性,当有人尝试登录您的系统时,他将被要求与您/您的应用分享他的照片或电子邮件。一旦他批准了这一点,您就可以访问其值:

profile.photos[0].value,
profile.emails[0].value,
...

对于电子邮件,有时更改有用:

passport.authenticate('facebook');

对此:

passport.authenticate('facebook', { scope: 'email'}));

答案 1 :(得分:8)

在profileFields中你应该使用电子邮件(plular)而不是电子邮件(单数):

profileFields: ['id', 'displayName', 'link', 'about_me', 'photos', 'emails']

这在facebook-passport文档README.md中注明:

  

profileFields参数,它指定字段列表(由Portable Contacts约定命名)

您可以找到passportjs here

的便携式联系人约定
相关问题