繁体   English   中英

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

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

我正在使用Facebook JavaScript SDK。 我可以使用以下方式获取用户的信息:

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

我得到用户的朋友使用:

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

我想得到朋友出生日期和地点。 是否有FB.api方法来获取它,或者我可以使用FB.Data.query获取它吗?

首先,您的应用需要具有正确的权限 ,在这种情况下, friends_birthdayfriends_location

接下来,使用Graph API请求中的fields参数来请求birthdaylocation字段:

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

根据某些朋友帐户的隐私设置,您可能会或可能无法访问生日或位置数据,因此请务必处理缺少信息的情况。 看到这个问题的原因。

这样做:

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

你会得到:

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

您可以使用收到的ID访问资源:

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

您需要具有user_birthdayfriends_birthdayuser_locationfriends_location权限的经过身份验证的用户 然后你可以这样做:

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

在这里尝试一下

您可以使用FB.api在数组中获取完整朋友的id和名称。 需要使用FB.init初始化应用程序,然后检查用户是否使用FB.getloginstatus()登录。 那么只有FB.api才有用。

这是一个链接 ,解释了如何使用Javascript SDK获取朋友。

暂无
暂无

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

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