使用 Facebook Graph API 获取用户好友列表 [英] Using the Facebook Graph API to get a list of a user's friends

查看:47
本文介绍了使用 Facebook Graph API 获取用户好友列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取应用好友列表中的用户,以过滤出现在好友选择器中的用户.我知道我可以拨打以下电话并获取列表:

I need to get a hold of a user of my app's list of friends to filter the users that show up in a friend picker. I know I can call the following and get the list:

https://graph.facebook.com/me/friends?access_token=<access_token>

我用自己的帐户在地址栏中试用了它,它似乎完全符合我的需要.问题是,我不知道如何在 js 文件本身中使用它.我尝试调用它并通过 jquery 调用获取数据,但它似乎没有返回任何有用的信息.

I tried it out in the address bar with my own account and it seems to work exactly as I need it to. Problem is, I don't know how to make use of it in a js file itself. I tried calling it and getting the data out with a jquery call but it doesn't seem to return anything helpful.

$.get("https://graph.facebook.com/me/friends",
    {access_token: <access_token>},
    function(data){ document.write("Data Loaded: " + data);});

我应该如何在我的 js 文件中调用它,然后实际使用这些信息?谢谢.

How should I be calling this in my js files and then actually make use of the information? Thanks.

推荐答案

UPDATE:根据 V2.0 中引入的变化/me/friends 将仅返回应用好友.

UPDATE: As per the changes introduced in V2.0, /me/friends will return app friends only.

正确的方法是使用 Facebook Javascript-SDK,类似这样:

The right way to do that is by using the Facebook Javascript-SDK, something like this:

function getFriends() {
    FB.api('/me/friends', function(response) {
        if(response.data) {
            $.each(response.data,function(index,friend) {
                alert(friend.name + ' has id:' + friend.id);
            });
        } else {
            alert("Error!");
        }
    });
}

请注意:

  1. 我也在这里使用 jQuery
  2. 在调用此函数之前,您可能需要检查用户是否已连接.

这篇关于使用 Facebook Graph API 获取用户好友列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆