简体   繁体   中英

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?

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.

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}

You'll need an authenticated user with the user_birthday , friends_birthday , user_location & 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 out here .

You can get complete friend's id and name in an array by using FB.api. One needs to initialize the app using FB.init and later checks if user is logged in using FB.getloginstatus(). then only FB.api is useful.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM