getting info about facebook user using passport

1k views Asked by At

this are my first steps in node.js in general and passport in particular and i came across a really annoying issue. i am trying to get the events user attended to with hes Facebook profile but no matter what i tried it simply didn't work. So i thought "ok, lets and get other data" but except for the basic display name and profile pic any other attempt (birthday,events,friends list etc..) ends up with no data. i tried using Facebook's api alot in the last few days (for the first time) and simply couldnt figure it out...this is my last attempt:

passport.use(new FacebookStrategy({
    clientID: config.fb.appID,
    clientSecret: config.fb.appSecret,
    callbackURL: config.fb.callbackURL,
    profileFields: ['id', 'displayName', 'photos', 'birthday', 'events', 'profileUrl', 'emails', 'likes']
}, function(accessToken, refershToken, profile, done){
    //Check if the user exists in our Mongo DB database
    //if not, create one and return the profile
    //if exists, return profile
    userModel.findOne({'profileID':profile.id}, function(err, result){
        if(result){
            done(null,result);
        } else {
            // Create a new user in our mongoLab account
            var newFbUSer = new userModel({
                profileID: profile.id,
                fullname: profile.displayName,
                profilePic:profile.photos[0].value || '',
                birthday:profile.birthday,
                //friends:profile.user.friends[0],
                profileUrl:profile.profileUrl

            });


            newFbUSer.save(function(err){
                done(null,newFbUSer);
                console.log(newFbUSer.displayName);
            })
        }
    })
}))  

any help with how can i get and use user's friends list/ events??

2

There are 2 answers

0
Ved Mulkalwar On

Maybe you haven't passed the details of information you need to facebook while calling the Facebook login. While calling the facebook login you need to specify what all information you need in the scope. For example if you need public_profile,email,user_friends following is the code which you will add in routes:

app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'public_profile,email,user_friends' }));
0
Weston On

Try this for the full name:

fullName: profile.name.givenName + ' ' + profile.name.familyName