Accessing Facebook friends list fails when used with JavaScript SDK

6k views Asked by At

I have been seeing questions on Stack Overflow to access the Facebook friends list of a user. I have tried in many different ways to do this and unfortunately nothing seems to be working for me.

Can any one tell me the exact problem I am facing? I'm totally new to javascript. Below is the code I am using

    <html>
    <head>
      <title>My Facebook Login Page</title>
    </head>
    <body>
     <div id="fb-root"></div>
     <script>
        window.fbAsyncInit = function() {
        var friends = new Array();
       FB.init({
      appId      : 'some_id', // App ID
      channelUrl : 'http://apps.facebook.com/myapp', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

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

    };



      // Load the SDK Asynchronously
     (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "http://connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
       }(document)); 
     </script>

    <p>
        <fb:profile-pic uid='loggedinuser' facebook-logo='true'/>
    </p>
    </br>
    </br>
    "Welcome, <fb:name uid='' useyou='false'></fb:name>
    </br>

    </br>
    </br>



 </br>
 </br>

    </br></br> <fb:login-button autologoutlink="true" />

 </body>
 </html>

I am getting the response as undefined and am not able to even login to facebook. I tried using the following sample http://jobyj.in/api/get-facebook-friends-using-javascript-sdk/. This works fine with the Demo but then I downloaded it and tried to run from my machine it is also not giving me any result.

Any suggestions for me?

Update

I am using the example given by @Somnath below and able to login. But after that getting the value undefined for response.session resulting in zero friends list. Any idea for this?

1

There are 1 answers

5
Somnath Muluk On

Here is tutorial for login through javascript.

Tutorial Javascript SDK

Ist try to log in. Otherwise you will get undefined response.

FB.getLoginStatus(function(response) {
         if (response.session) {
             // logged in and connected user, someone you know
            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!");
                  }
             });
         }
     });

You will get friend list of logged in user.

Updated Answer: Don't specify channelUrl at time of initialization. Only give only following four parameters. Try this out.

FB.init({
  appId      : 'some_id', // App ID
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true  // parse XFBML
});

Update2: Try using fql:

if (response.session) {  

//getting current logged in user's id from session object  

 globaluserid=response.session["uid"];  

 //fetching friends uids from 'friend' table. We are using FB.api syntax  

 FB.api(  

 {  

 method: 'fql.query', 

 query: 'SELECT uid1 FROM friend WHERE uid2='+globaluserid 

 }, 

 function(response) { 

 //once we get the response of above select query we are going to parse them 

 for(i=0;i<response.length;i++) 

 { 

 //fetching name and square profile photo of each friends 

 FB.api( 

 { 

 method: 'fql.query',  

 query: 'SELECT name,pic_square FROM user WHERE uid='+response[i].uid1 

 }, 

 function(response) { 

 //creating img tag with src from response and title as friend's name  

 htmlcontent='<img src='+response[0].pic_square+' title='+response[0].name+' />';  

 //appending to div based on id. for this line we included jquery  

 $("#friendslist").append(htmlcontent);  

 }  

 );  

 }  

 }  

 );  

 } else {  

 // no user session available, someone you dont know  

 top.location.href="../kfb_login.php";  

 }