Search code examples
facebookfacebook-graph-apimeteormeteor-accounts

Meteor Facebook login (Meteor.loginWithFacebook) issue extracting public profile, email and user_friends


Trying to get Meteor Facebook login to work. It functions fully in that it uses Facebook API and requests the correct permissions from the users account and then logs in successfully.

The problem is it doesn't save the permission requested information even though its been approved and only the basic name and ID are available in Meteor.user().services.facebook. Is this code not working because it's not saving the users details on login? I can't find a resource that details how to save or extract the other data.

Simply trying to console log the data to see that it's been extracted out of the Facebook user account on log in.

Within Meteor.isClient code:

Template.login.events({
'click #facebook-login': function(event) {
    Meteor.loginWithFacebook({ requestPermissions: ['email', 'public_profile', 'user_friends', 'user_likes']}, function(err){
        if (err) {
            throw new Meteor.Error("Facebook login failed");
        }
        console.log(Meteor.user().services.facebook.name);
        console.log(Meteor.user().services.facebook.id);
        console.log(Meteor.user().services.facebook.email);
        console.log(Meteor.user().services.facebook.gender);            
    });
},

'click #logout': function(event) {
    Meteor.logout(function(err){
        if (err) {
            throw new Meteor.Error("Logout failed");
        }
    });
}

The config code:

ServiceConfiguration.configurations.remove({
    service: 'facebook'
});

ServiceConfiguration.configurations.insert({
    service: 'facebook',
    appId: 'correctAppID',
    secret: 'CorrectSecret'
});

Solution

  • For Facebook v2.4 API after you have requested for certain permissions you can then access them by making a graph API call and requesting them with a valid auth token. The code is as follows:

    if (user.hasOwnProperty('services') && user.services.hasOwnProperty('facebook')  ) {
            var result = Meteor.http.get('https://graph.facebook.com/v2.4/' + user.services.facebook.id + '?access_token=' + user.services.facebook.accessToken + '&fields=first_name, last_name, birthday, email, gender, location, link, friends');
    
            console.log(result.data.first_name);
            console.log(result.data.last_name);
            console.log(result.data.birthday);
            console.log(result.data.email);
            console.log(result.data.gender);
            console.log(result.data.location);
            console.log(result.data.link);
            console.log(result.data.friends);
    }