Javascript 使用 Facebook Graph API 获取用户好友列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5317833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:38:19  来源:igfitidea点击:

Using the Facebook Graph API to get a list of a user's friends

javascriptfacebookfacebook-graph-api

提问by keybored

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>

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.

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

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

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

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

回答by ifaour

UPDATE: As per the changes introduced in V2.0, /me/friendswill return app friendsonly.

更新:根据V2.0 中引入更改/me/friends将仅返回应用好友



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

正确的方法是使用Facebook Javascript-SDK,如下所示:

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!");
        }
    });
}

Please note that:

请注意:

  1. I'm using jQuery here too
  2. You may need to check if the user is connected before calling this function.
  1. 我在这里也使用 jQuery
  2. 在调用此函数之前,您可能需要检查用户是否已连接。

回答by Manuel Pedrera

If you want to extract information from the Graph with JavaScript, you will have to use the JS SDKand FB.apimethod to make the calls.

如果要使用 JavaScript 从 Graph 中提取信息,则必须使用JS SDKFB.api方法进行调用。

回答by Alfa Net Cell

It's working for me :

它对我有用:

<!DOCTYPE html> <html lang="en"> <head>
    <meta charset="utf-8" />
    <title>JSON Sample</title> </head> <body>
    <div id="placeholder"></div>
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script>   $.getJSON('https://graph.facebook.com/eris.risyana/friends?limit=100&access_token=[your_access_token]', function(mydata) {
        var output="<ul>";
        for (var i in mydata.data) {
            output+="<li>NAMA : " + mydata.data[i].name + "<br/>ID : " + mydata.data[i].id + "</li>";
        }

        output+="</ul>";
        document.getElementById("placeholder").innerHTML=output;   });
    </script> </body> </html>

Result :

结果 :

NAMA : Priscillia Anna Yuliana
ID : 534513444
NAMA : Priyatna Mii
ID : 534619517
NAMA : Icha Sasmita
ID : 544737437
NAMA : CW Trianggono
ID : 599225957
NAMA : Krshna Sulanjana
ID : 605633569
NAMA : Aris Pujiarti
ID : 635209234
NAMA : Armand Muljadi
ID : 663419616
NAMA : Nurdin Somantri
ID : 675697956
NAMA : Muhammad Zamzam Fauzanafi
ID : 686838979
NAMA : Jerome Coutelier
ID : 690661663