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.
I barely know anything about FB API but have you looked at this:
http://developers.facebook.com/docs/reference/javascript/FB.Data.query
And this:
http://developers.facebook.com/docs/reference/fql/
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.
精彩评论