javascript 在js api中获取facebook好友列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25880049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:10:13  来源:igfitidea点击:

Get facebook friends list in js api

javascriptfacebookjsapi

提问by Vinay

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.com/' + 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.com/' + 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.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
    </script>

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

用户登录但无法显示 Facebook 好友列表。

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

代码在新 sdk 之前运行。在我/朋友之前使用 invitable_friends。

anyone please help.

任何人请帮忙。

and also is there any way besides this ?

除此之外还有什么办法吗?

回答by luschn

You need to ask for user_friendsin the authorization process:

您需要user_friends在授权过程中要求:

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

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

你需要这个 for/me/friends和 for /me/invitable_friends,请参阅 Facebook 文档:https: //developers.facebook.com/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.com/docs/apps/changelog

虽然你只会得到使用该应用程序的朋友/me/friends。有关更多信息,请参阅变更日志:https: //developers.facebook.com/docs/apps/changelog

What CBroe commented in your question is very important too. invitable_friendsis for Games on facebook.com only.

CBroe 在您的问题中评论的内容也非常重要。invitable_friends仅适用于 facebook.com 上的游戏。

回答by Murad Hasan

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:

查看文档说明#reference-user_friends

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

user_friends-提供对也使用您的应用程序的人的朋友列表的访问。

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

之后,您将能够看到您想要的内容。

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