epsilones
epsilones

Reputation: 11629

Displaying Friends with Javascript SDK

On my website I would like to display the users Facebook friends, using the Facebook Javascript SDK. When loading the asynchronous script, I use this code :

window.fbAsyncInit = function() {               
  FB.init({appId: 'MYAPPID', status: true, cookie: true, xfbml: true});

  FB.api('/me/friends', function(response){
      var divTarget=document.getElementById("friends-list-container");
      var data=response.data;
      for (var friendIndex=0; friendIndex<data.length; friendIndex++)
    {
           var divContainer = document.createElement("div");
           divContainer.innerHTML="<b>" + data[friendIndex].name + "</b>";
           divTarget.appendChild(divContainer);
    }
  });  
};

I think I have asked for the right perms: 'friends_about_me', but it doesn't work.

Does anyone have any ideas?

Best, Newben

Upvotes: 1

Views: 3565

Answers (1)

Juicy Scripter
Juicy Scripter

Reputation: 25938

You doesn't need to ask for friends_about_me to get friends of the user, but you for sure should authorize the user (which is not done in the code you provided), also due to asynchronous nature of status retrieval you must ensure user is already logged in prior to fetching his friends.

Also it wouldn't hurt to add some error checking in your callback:

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    FB.api('/me/friends', function(response){
      if (response && response.data){
        // Handle response
      } else {
        console.log('Something goes wrong', response);
      }
    });
  }
});

Upvotes: 1

Related Questions