0

I log my users with Facebook using Passport.js and then I want to get their friends list. I use FbGraph to request Facebook graph API.

So far, so well. Facebook graph API results are paged, but I can't manage to go beyond the first page. Then responses are empty.

Here's the code:

var graph = require('fbgraph');

// accessToken is returned by Express
graph.setAccessToken(accessToken);

var getFriends = function(url) {
    graph.get(url, function (err, res) {
        console.log(res);
        if (res.paging && res.paging.next) {
            getFriends(res.paging.next);
        }
    });
};

getFriends('/me/friends');

And the output:

{ data: 
    [ { name: 'Gonzague xxx', id: 'xxx' },
    { name: 'Cyprien xxx', id: 'xxx' },
    { name: 'Chloé xxx', id: 'xxx' },
    { name: 'Elena xxx', id: 'xxx' },
    { name: 'Sari xxx', id: 'xxx' },
    { name: 'Marie xxx', id: 'xxx' } ],
    paging: { next: 'https://graph.facebook.com/v2.0/xxx/friends?access_token=xxx&limit=5000&offset=5000&__after_id=enc_xxx' },
    summary: { total_count: 424 } }
    404
    { data: [],
    paging: { previous: 'https://graph.facebook.com/v2.0/xxx/friends?access_token=xxx' },
    summary: { total_count: 424 } 
}
Buzut
  • 4,875
  • 4
  • 47
  • 54

1 Answers1

2

Unfortunately, you can't get the whole friends list on Graph API 2.0+, you can only get your friends who are using your app (using means, have granted permission to that app on Facebook). The total_count key means the user has 424 friends, it doesn't mean that you can get all 424 friends from your app.

The list you are seeing is just the people who have authorized your app, and since there are only a few people there, pagination yields an empty result as what you see is all you have.

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • Really? I had the same implementation in PHP and it returned the whole friendList. Is there a way to do so in JS (maybe by using a previous version of the API…)? – Buzut Aug 15 '15 at 13:36
  • 1
    @user1717735 When was it? If it was before forced Graph API 2.0 migration (April 30, 2015) then it was possible, now, unfortunately not. What you *can* try is to look at the "all_mutual_friends" edge introduced in Graph API 2.3, it may provide what you are looking for, if somehow mutual friend information between two app users is enough. See new features of Graph API 2.3 here: https://developers.facebook.com/docs/apps/changelog – Can Poyrazoğlu Aug 15 '15 at 13:40
  • Yep the last time i tried the PHP implementation was before April. Then, for a test app, how can you test if you don't have any friend that use the app (not gonna say to my friends "hey log into this so I can make tests!"…)? – Buzut Aug 15 '15 at 13:45
  • Another [SO post](http://stackoverflow.com/questions/23417356/facebook-graph-api-v2-0-me-friends-returns-empty-or-only-friends-who-also-u) on the matter – Buzut Aug 15 '15 at 13:49
  • 1
    @user1717735 you can create test users at the developer console of your app. – Can Poyrazoğlu Aug 15 '15 at 13:56