Get facebook friends list in js api
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 logged in but Failed to display facebook friends list.
The code worked before the new sdk. using invitable_friends before it was me / friends.
someone please help.
and also is there any other way?
source to share
You need to ask user_friends
during the authorization process:
FB.login(function(response) {
if (response.authResponse) {
window.location.reload();
}
}, {scope:'email,user_friends'});
It is necessary for both /me/friends
, and to /me/invitable_friends
see the documents in Facebook format:. Https://developers.facebook.com/docs/games/invitable-friends/v2.1
Although you will get friends who also use the app with /me/friends
. See Changelog for more information: https://developers.facebook.com/docs/apps/changelog
What CBroe has commented in your question is very important. invitable_friends
only for playing games on facebook.com.
source to share
If your app is public and is also shared by some of the friends who use the app, you can get a list of them.
See what the # reference-user_friends documentation means :
user_friends . Provides access to a list of friends of friends who also use your app.
Then you can see what you want.
FB.api('/me/friends', function(response){
console.log(response);
}, {scope: 'user_friends'});
source to share