Facebook Graph returns a different id than what is saved using Passport-facebook

Asked

Viewed 114 times

1

I have an application where the user logs in using the facebook account, saving the id, name and email. I am using https://github.com/jaredhanson/passport-facebook Excerpt:

//Cria um usuario do facebook ou vincula a um usuario ja existente
UserSchema.statics.findOrCreateFaceBookUser = function(profile, done) {
    var User = this;
    User.findOne({
        'email': profile.emails[0].value
    }, function(err, user) {
        if (err) {
            throw err;
        }
        if (user) {
            user.facebook = {
                id: profile.id,
                email: profile.emails[0].value,
                name: profile.displayName
            };
            user.save(function(err) {
                if (err) {
                    return next(err);
                } else {
                   // done(null, user);
                }
            });
            done(null, user);
        } else {
            User.findOne({
                'facebook.id': profile.id
            }, function(err, user) {
                if (err) {
                    throw err;
                }
                //if (err) return done(err);
                if (user) {
                    done(null, user);
                } else {
                    User.create({
                        firstName:profile.name.givenName,
                        lastName:profile.name.familyName,
                        email: profile.emails[0].value,
                        gender:profile.gender,
                        image:"http://graph.facebook.com/"+profile.id+"/picture?type=normal",
                        facebook: {
                            id: profile.id,
                            email: profile.emails[0].value,
                            name: profile.displayName
                        }
                    }, function(err, user) {
                        if (err) {
                            throw err;
                        }
                        done(null, user);
                    });
                }
            });
        }

    });


};

The problem is that I need to fetch facebook friends who are also using the application, but the id returned by facebook Graph does not match what I’m persisting in the application.

https://graph.facebook.com/me/friends?fields=id,name&access_token=foo

Any idea?

1 answer

1

Nickolas, this application of yours was created before or after the introduction of API 2.0?
Remember that now the returned ID is the app-scoped ID and no longer the Facebook ID.
If so, do the upgrade of your application or call the /me/friendsusing the v1.0 or v2.0 as appropriate.

Take a look at https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids

Browser other questions tagged

You are not signed in. Login or sign up in order to post.