简体   繁体   English

如何使用JavaScript SDK获取Facebook用户的朋友信息

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

I am using the Facebook JavaScript SDK. 我正在使用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? 是否有FB.api方法来获取它,或者我可以使用FB.Data.query获取它吗?

First your app needs to have the correct permissions , friends_birthday and friends_location in this case. 首先,您的应用需要具有正确的权限 ,在这种情况下, friends_birthdayfriends_location

Next, use the fields parameter in your Graph API request to request the birthday and location fields: 接下来,使用Graph API请求中的fields参数来请求birthdaylocation字段:

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: 您可以使用收到的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 . 您需要具有user_birthdayfriends_birthdayuser_locationfriends_location权限的经过身份验证的用户 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. 您可以使用FB.api在数组中获取完整朋友的id和名称。 One needs to initialize the app using FB.init and later checks if user is logged in using FB.getloginstatus(). 需要使用FB.init初始化应用程序,然后检查用户是否使用FB.getloginstatus()登录。 then only FB.api is useful. 那么只有FB.api才有用。

Here is a link which explains how to fetch friends using Javascript SDK. 这是一个链接 ,解释了如何使用Javascript SDK获取朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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