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

49,500

Solution 1

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


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

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.

Solution 2

If you want to extract information from the Graph with JavaScript, you will have to use the JS SDK and FB.api method to make the calls.

Solution 3

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
Share:
49,500

Related videos on Youtube

keybored
Author by

keybored

What is this I don't even?

Updated on August 09, 2020

Comments

  • keybored
    keybored over 3 years

    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.

    $.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.

  • keybored
    keybored about 13 years
    Both answers were right but this one wins the prize for sure for the example. Thanks a bunch!
  • Severino Lorilla Jr.
    Severino Lorilla Jr. over 9 years
    this solution will only pull those users who have already authorized the application. Anyone got a solution that will pull all users in my friend's list?
  • ifaour
    ifaour over 9 years
    @severinolorillajr, this is not possible.
  • Shilpa
    Shilpa over 7 years
    @Abhinay: With this approach, I faced issues with the access token and hence started using the JS SDK with my application.
  • Abhinay
    Abhinay over 7 years
    @Shilpa what I want to know is whether you are able to fetch the entire list of Facebook friends ?

Related