passport-facebook in nodejs - profile fields



我在登录用户时试图从Facebook获得的一些配置文件字段没有通过。

我在节点中使用passportjs。这就是facebook的策略:

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.get('/auth/facebook', passport.authenticate('facebook', { scope: ['user_about_me', 'email'] }));

结果是,"链接"、"aboutme"one_answers"电子邮件"不会被拉取,而其他字段则被拉取。

profileFields参数符合PortableContacts约定。这意味着您希望使用"电子邮件"而不是"电子邮件"。至于"about_me"字段,passport facebook似乎并不完全支持OpenSocial协议。这意味着,如果您想对这两个概要文件元素都使用"profileFields"参数,那就太倒霉了。以下代码片段取自master分支,说明了这种限制:
Strategy.prototype._convertProfileFields = function(profileFields) {
    var map = {
    'id':          'id',
    'username':    'username',
    'displayName': 'name',
    'name':       ['last_name', 'first_name', 'middle_name'],
    'gender':      'gender',
    'profileUrl':  'link',
    'emails':      'email',
    'photos':      'picture'
};
...

此映射中列出的字段是目前唯一受支持的字段。

幸运的是,一切都没有失去。如果您选择而不是来使用profileFields参数,那么奇怪的是,您将通过一个名为"bio"的属性收到您感兴趣的"about_me"内容。以下是您可以访问的方法:

passport.use(new FacebookStrategy({
  clientID: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET,
  callbackURL: FACEBOOK_CALLBACK_URL
},
function(accessToken, refreshToken, profile, done) {
   console.log("bio: " + profile._json.bio);
}));

不幸的是,这并没有给你提供你感兴趣的其他数据。我猜,在你的情况下,你可能会考虑在passport facebook回调期间收集支持的约定字段,然后在后续调用中直接使用facebookapi获取扩展的配置文件字段。要么这样,要么戳护照的脸书维护者,以扩大他们的现场支持。

aturkelson是正确的。还不支持about_me。就电子邮件而言,只要你要求,它就会附带个人资料。我还有一个控制台日志来确认我没有疯。

//Passport facebook strategy
exports.passportInit= passport.use(new facebookStrategy({
clientID: process.env.FACEBOOK_APP_ID ,
clientSecret: process.env.FACEBOOK_SECRET_ID,
callbackURL: '/api/auth/facebook/callback',
profileFields: ['id', 'displayName', 'emails', 'photos']
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
 db.User.findOne({facebook_id: profile.id}, function(err, oldUser){
     if(oldUser){
         done(null,oldUser);
     }else{
         var newUser = new db.User({
             facebook_id : profile.id,
             facebook_photo : profile.photos[0].value,
             email : profile.emails[0].value,
             display_name : profile.displayName,
             // picture: profile.picture
         }).save(function(err,newUser){
             if(err) throw err;
             done(null, newUser);
         });
     }
 });
}
));

据我所知,FB正在为您提供信息。。。试试这段代码。。

passport.use(new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: FACEBOOK_CALLBACK_URL,
    profileFields: ['id', 'displayName', 'email'] }, 
         function(accessToken, refreshToken, profile, done) {
         // asynchronous
         process.nextTick(function() {
            FACEBOOK_TOKEN = accessToken;
            FACEBOOK_USER = profile._json;

            // facebook can return multiple emails so we'll take the first
            profile.emails[0].value;
            console.log(FACEBOOK_USER.email);
            done(null, profile);
      });
  }));

最新更新