2015-09-16 38 views
0

我在Facebook上創建了一個應用程序。我想將該應用程序引用到朋友,以便我想顯示朋友列表。我只收到用戶信息,但沒有收到朋友列表。使用圖示API獲取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!"); 
     } 
    }); 
} 

回答

0

你甚至不要求你的代碼中的朋友。自Graph API v2.0以來,您只會收到也在使用您的應用的朋友,而不是所有朋友。

使用

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

謝謝你的解決方案。但我堅持在一個點,即使得到朋友的數量,但不是朋友的名字和細節。請幫助我瞭解如何在Facebook應用中獲取朋友的詳細信息。我正在使用圖形API的2.4版本 - 可能是這個信息幫助你回答我的問題。 – Syeda

+0

再次閱讀我的答案的第二句。 – Tobi

-1

在應用中跟朋友

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

doc


不能生存友(僅在遊戲)

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

doc

+0

'invitable_friends'只能用於遊戲。 – Tobi

+0

@Tobi謝謝你提醒我! – omxian