Rahul Soni
Rahul Soni

Reputation: 1

FB.api not working every time

I am new to Facebook Javascript SDK. I am trying to create an application using this API. But has hard luck while usign the FB.api method. Somehow my FB.api method is not working properly each time. Sometimes it will give the response (I am just accessing response.name) while other time it will not invoked properly.

window.fbAsyncInit = function() {
FB.init({
  appId      : 'my_app_id', // App ID
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  oauth      : true, // enable OAuth 2.0
  xfbml      : true  // parse XFBML
});
FB.getLoginStatus(function(response) {
  if (response.authResponse) {
   alert('user is:'+response.status); 
   userId = response.authResponse.userID;
   FB.api('/me',function(response) {
   alert('Inside FB.api function');
   document.getElementById("userInfo").innerHTML = 'Welcome <img src="https://graph.facebook.com/'+ response.id + '/picture" style="margin-right:5px"/>' +response.name + '!';
    });
    }
});
};

Other methods FB.ui and FB.getLoginStatus and Facebook like button is working as expected, but only FB.api is not working seamless manner as others. Do not why? Please help me to idenify the problem.

Upvotes: 0

Views: 4387

Answers (3)

Pierre Maoui
Pierre Maoui

Reputation: 6384

I had the same problem. FB.api just stopped working silently at some point in my app. I noticed it was due to a DOM manipulation of :

<div id="fb-root"></div>

Either a duplication or anything. You should check that point. It looks like Johan Strydom's problem.

Upvotes: 0

Johan Strydom
Johan Strydom

Reputation: 41

I had the exact same problem. This answer pointed me into the right direction:

FB.api does not work when called right after FB.init

To solve my problem I had to subscribe to the correct event. In my case it was 'auth.authResponseChange' after FB.init

FB.Event.subscribe('auth.authResponseChange', function(){
    FB.api('/me',function(r){
    console.log(r.id);
     if (!r || r.error) {
        alert('Error occured');
      } else {
        alert(r.name);
      }         
    });
});

Upvotes: 3

Abby
Abby

Reputation: 3199

That code works fine for me, but you might want to try renaming your response variables. Inside the FB.api callback, you actually have two response variables which will undoubtedly cause problems. Try changing them and see if you get different results.

FB.getLoginStatus(function(loginResponse) {
  if (loginResponse.authResponse) {
   alert('user is:'+loginResponse.status); 
   userId = loginResponse.authResponse.userID;
   FB.api('/me',function(apiResponse) {
   alert('Inside FB.api function');
   document.getElementById("userInfo").innerHTML = 'Welcome <img src="https://graph.facebook.com/'+ apiResponse.id + '/picture" style="margin-right:5px"/>' +apiResponse.name + '!';
    });
    }
});

Its possible there is something else on your page affecting this. Do you have a link to your page?

Upvotes: 0

Related Questions