最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get facebook friends list in js api - Stack Overflow

programmeradmin1浏览0评论
I am getting user info but unable to get friend list.    

<script>
    function sortMethod(a, b) {
        var x = a.name.toLowerCase();
        var y = b.name.toLowerCase();
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }

    window.fbAsyncInit = function() {
        FB.init({ appId: 'xxxxxxxxx', 
            status: true, 
            cookie: true,
            xfbml: true,
            oauth: true
        });

        function updateButton(response) {
            var button = document.getElementById('fb-auth');

            if (response.authResponse) { // in case if we are logged in
                var userInfo = document.getElementById('user-info');
                FB.api('/me', function(response) {
                    userInfo.innerHTML = '<img src="/' + response.id + '/picture">' + response.name;
                    button.innerHTML = 'Logout';
                });

                // get friends
                FB.api('/me/invitable_friends', function(response) {
                    var result_holder = document.getElementById('result_friends');
                    var friend_data = response.data.sort(sortMethod);

                    var results = '';
                    for (var i = 0; i < friend_data.length; i++) {
                        results += '<div><img src="/' + friend_data[i].id + '/picture">' + friend_data[i].name + '</div>';
                    }

                    // and display them at our holder element
                    result_holder.innerHTML = '<h2>Result list of your friends:</h2>' + results;
                });

                button.onclick = function() {
                    FB.logout(function(response) {
                        window.location.reload();
                    });
                };
            } else { // otherwise - dispay login button
                button.onclick = function() {
                    FB.login(function(response) {
                        if (response.authResponse) {
                            window.location.reload();
                        }
                    }, {scope:'email'});
                }
            }
        }

        // run once with current status and whenever the status changes
        FB.getLoginStatus(updateButton);
        FB.Event.subscribe('auth.statusChange', updateButton);    
    };

    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
    </script>

user getting logged in but Unable to display facebook friends list.

Code was working before new sdk. using invitable_friends before it was me/friends.

anyone please help.

and also is there any way besides this ?

I am getting user info but unable to get friend list.    

<script>
    function sortMethod(a, b) {
        var x = a.name.toLowerCase();
        var y = b.name.toLowerCase();
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }

    window.fbAsyncInit = function() {
        FB.init({ appId: 'xxxxxxxxx', 
            status: true, 
            cookie: true,
            xfbml: true,
            oauth: true
        });

        function updateButton(response) {
            var button = document.getElementById('fb-auth');

            if (response.authResponse) { // in case if we are logged in
                var userInfo = document.getElementById('user-info');
                FB.api('/me', function(response) {
                    userInfo.innerHTML = '<img src="https://graph.facebook./' + response.id + '/picture">' + response.name;
                    button.innerHTML = 'Logout';
                });

                // get friends
                FB.api('/me/invitable_friends', function(response) {
                    var result_holder = document.getElementById('result_friends');
                    var friend_data = response.data.sort(sortMethod);

                    var results = '';
                    for (var i = 0; i < friend_data.length; i++) {
                        results += '<div><img src="https://graph.facebook./' + friend_data[i].id + '/picture">' + friend_data[i].name + '</div>';
                    }

                    // and display them at our holder element
                    result_holder.innerHTML = '<h2>Result list of your friends:</h2>' + results;
                });

                button.onclick = function() {
                    FB.logout(function(response) {
                        window.location.reload();
                    });
                };
            } else { // otherwise - dispay login button
                button.onclick = function() {
                    FB.login(function(response) {
                        if (response.authResponse) {
                            window.location.reload();
                        }
                    }, {scope:'email'});
                }
            }
        }

        // run once with current status and whenever the status changes
        FB.getLoginStatus(updateButton);
        FB.Event.subscribe('auth.statusChange', updateButton);    
    };

    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
    </script>

user getting logged in but Unable to display facebook friends list.

Code was working before new sdk. using invitable_friends before it was me/friends.

anyone please help.

and also is there any way besides this ?

Share Improve this question asked Sep 16, 2014 at 23:38 VinayVinay 391 gold badge1 silver badge3 bronze badges 2
  • What do you need the list of friends for? With API v2, you only get a user’s friends that are also using the same app. And invitable_friends is for use by canvas apps in the Games category only; if you’re app isn’t in that category, then don’t misuse invitable_friends for anything else. – C3roe Commented Sep 16, 2014 at 23:45
  • I am making simple app just unable to get friends list. – Vinay Commented Sep 16, 2014 at 23:48
Add a ment  | 

2 Answers 2

Reset to default 3

You need to ask for user_friends in the authorization process:

FB.login(function(response) {
    if (response.authResponse) {
        window.location.reload();
    }
}, {scope:'email,user_friends'});

You need this both for /me/friends and for /me/invitable_friends, see Facebook docs: https://developers.facebook./docs/games/invitable-friends/v2.1

Although you will only get friends who are using the App too with /me/friends. See changelog for more information: https://developers.facebook./docs/apps/changelog

What CBroe mented in your question is very important too. invitable_friends is for Games on facebook. only.

If your app is public and live also some of the friends who are using the app then you can get the list of them.

See what the documentation state #reference-user_friends:

user_friends - Provides access to a person's list of friends that also use your app.

After that you will be able to see what you want.

FB.api('/me/friends', function(response){
    console.log(response);
}, {scope: 'user_friends'});
发布评论

评论列表(0)

  1. 暂无评论