使用图表API在js中获取Facebook好友列表

时间:2015-09-16 06:23:57

标签: javascript facebook

我在Facebook上创建了一个应用程序。我想将该应用程序推荐给朋友,以便我想显示朋友列表。我只收到用户信息,但没有收到好友列表。

我使用了下面的代码,请帮我搞好朋友列表。

window.fbAsyncInit = function() {
    FB.init({
      appId      : 'xxxxxxxxxxxxxxxxxxx',
      xfbml      : true,
      version    : 'v2.4'
    });

function onLogin(response) {
  if (response.status == 'connected') {
    FB.api('/me?fields=first_name', function(data) {
      var welcomeBlock = document.getElementById('fb-welcome');
      welcomeBlock.innerHTML = 'Hello, ' + data.first_name + '!';
    });
  }
}

FB.getLoginStatus(function(response) {
  // Check login status on load, and if the user is
  // already logged in, go directly to the welcome message.
  if (response.status == 'connected') {
    onLogin(response);
  } else {
    // Otherwise, show Login dialog first.
    FB.login(function(response) {
      onLogin(response);
    }, {scope: 'user_friends, email'});
  }
});

  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

function getFriends() {
    alert("in friends");
    FB.api('/me/friends', 'GET',{},
    function(response) {
         if (response.data) {
             console.log(response.data);
             $.each(response.data,function(index,friend) {
              console.log(friend.name +'has id:'+friend.id);
             // alert(friend.name);
            });
        } else {
            alert("Error!");
        }
    });
}

2 个答案:

答案 0 :(得分:0)

您甚至没有在代码中请求好友。从Graph API v2.0开始,您只会收到同时使用您应用的朋友,而不是所有朋友。

使用

FB.api('/me?fields=first_name,friends', function(data) { ... });

答案 1 :(得分:-1)

在App好友

FB.api("/me/friends",function(res) { console.log(res) });

doc

诚邀朋友(仅限游戏)

FB.api("/me/invitable_friends",function(res){ console.log(res) });

doc