How to Get Facebook Friend Information Using the JavaScript SDK

I am using the JavaScript JavaScript SDK. I can get user information using:

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

I get user 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 place. Is there a way for FB.api to get it, or can I get it using FB.Data.query?

+7
javascript facebook
source share
5 answers

First, your application must have the correct permissions , friends_birthday and friends_location in this case.

Then use the fields parameter in the Graph API request to request the birthday and location fields:

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

Depending on the privacy settings on some friends accounts, you may or may not have access to birthday or location data, so make sure you handle cases where information is missing. See this question for what.

+8
source share

Doing:

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

You'll get:

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

Than access to the resource can be obtained using the identifier obtained:

 FB.api('/xxxxx', function(response) { ... }); // {first_name: "Name", gender: "male", id: "xxxxxx",.. etc} 
+5
source share

You will need an authenticated user with user_birthday , friends_birthday , user_location and friends_location permissions . Then you can do something like:

 FB.api('/me/friends', { fields: 'name,id,location,birthday' }, function(result) { // resut has the data }) 

Try it here .

+4
source share
0
source share

You can get the full identifier and friend name in the array using FB.api. You need to initialize the application using FB.init, and later checks to see if the user is registered with FB.getloginstatus (). then only FB.api is useful.

Here is a link that explains how to fetch friends using the Javascript SDK.

0
source share

All Articles