passport-facebook in nodejs - profile fields
Asked Answered
K

3

7

some of the profile fields i am trying to get from Facebook when logging in a user, are not going through.

I am using passportjs in node. this is the facebook strategy:

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
));

being used with:

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

the result is that 'link', 'about_me' and 'email' are not getting pulled while the other fields are.

Kerrin answered 8/12, 2013 at 19:22 Comment(0)
Z
10

The profileFields parameter adheres to the Portable Contacts convention. This means you would want to use 'emails' instead of 'email'. As for the "about_me" field, it does not appear as if passport-facebook supports the OpenSocial protocol fully. This means you're out of luck if you want to use the "profileFields" parameter for both of these profile elements. The following code snippet, taken from the master branch, illustrates this limitation:

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'
};
...

The fields listed in this mapping are the only ones supported right now.

Fortunately all is not lost. If you choose not to use the profileFields parameter then oddly enough you will be sent the "about_me" content you are interested in via a property called "bio". Here's how you could access that:

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);
}));

Unfortunately this doesn't give you the other data you were interested in. I'm guessing that in your situation you are probably looking at gathering the supported convention fields during the passport-facebook callback and then grabbing the extended profile fields in a followup call using the facebook api directly. Either that or poke the passport-facebook maintainers to extend their field support.

Zepeda answered 3/3, 2014 at 3:56 Comment(0)
A
3

aturkelson is correct. about_me is not supported yet. As far as email it comes with the profile as long as you request it. I also have a console log to confirm I am not crazy.

//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);
         });
     }
 });
}
));
Alleluia answered 30/1, 2018 at 20:49 Comment(0)
M
0

According to my knowledge FB is providing you the info... try this piece of code..

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);
      });
  }));
Mohr answered 17/1, 2017 at 15:15 Comment(1)
its confusing that profileFields treats emails and email as the same thing... but both seem validMckenney

© 2022 - 2024 — McMap. All rights reserved.