Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Facebook user's friends information using JavaScript SDK

I am using the Facebook JavaScript SDK. I am able to get the user's information using:

FB.api('/me', function(response) {
  ....
}); 

I get the user's friends using:

FB.api('/me/friends', function(response) {
 ...
}); //.. it only has two values name and id.

I want to get friends Date of birth and location. Is there a FB.api method to get it or can I get it using FB.Data.query?

like image 838
Nick Avatar asked Jul 24 '10 08:07

Nick


2 Answers

First your app needs to have the correct permissions, friends_birthday and friends_location in this case.

Next, use the fields parameter in your Graph API request to request the birthday and location fields:

FB.api('/me/friends', {fields: 'name,id,location,birthday'}, function(response) {
  //...
});

Depending on privacy settings on some of the friends' accounts, you may or may not be able to access birthday or location data, so make sure you handle the cases where the info is missing. See this question for why.

like image 169
jches Avatar answered Sep 30 '22 00:09

jches


Doing:

FB.api('/me/friends', function(response) {
 ...
}); //.. it only has two values name and id.

You will get:

{data: [{id: "xxxxx", name: "Friend name"}, {id: "xxxxx", name: "Friend name"}]}

Than you could access the resource using received id:

FB.api('/xxxxx', function(response) {
 ...
}); // {first_name: "Name", gender: "male", id: "xxxxxx",.. etc}
like image 41
Anton Avatar answered Sep 30 '22 01:09

Anton