【问题标题】:Get facebook friends list in js api在 js api 中获取 facebook 好友列表
【发布时间】:2014-11-10 20:50:58
【问题描述】:
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>

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

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

请大家帮忙。

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

【问题讨论】:

  • 你需要好友列表有什么用?使用 API v2,您只能获得也在使用同一应用程序的用户朋友。 invitable_friends 仅供游戏类别中的画布应用使用;如果您的应用不在该类别中,请不要将invitable_friends 用于其他任何用途。
  • 我正在制作简单的应用,只是无法获取好友列表。

标签: javascript facebook jsapi


【解决方案1】:

需要在授权过程中询问user_friends

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

/me/friends/me/invitable_friends 都需要这个,请参阅 Facebook 文档:https://developers.facebook.com/docs/games/invitable-friends/v2.1

虽然您只会通过/me/friends 获得同样使用该应用程序的朋友。有关更多信息,请参阅更新日志:https://developers.facebook.com/docs/apps/changelog

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

【讨论】:

    【解决方案2】:

    如果您的应用是公开的并且还有一些正在使用该应用的朋友,那么您可以获得他们的列表。

    查看文档状态#reference-user_friends

    user_friends - 提供对也使用您的应用的朋友列表的访问权限。

    之后你就可以看到你想要的了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多