Question

I'm dealing with this error couple of days. It used to work fine the same code, but now I'm stuck with this error. I've changed a host since then. Maybe Could be that? When I log in with my Facebook account, and the application is approved, I still can't retrieve user id and other default data.

Here is the script, please suggest anything:

<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script src="http://www.oneslike.me/js/jquery-1.6.2.min.js"></script>           
<script>
 $(document).ready(function(){           
    FB.init({
      appId      : 'YOUR API KEY HERE',
      status     : true, 
      cookie     : true,
      xfbml      : true,
      oauth      : true,
    });

    FB.Event.subscribe('auth.login', function(response) {
      window.location.reload(); //When logged reload
    });         
    FB.api('/me', function(user){ 
        if(user.id!=null){
            alert("Logged user with FB_id: "+user.id);
            $("#login_div").hide(); $("#data_div").show();
            var user_data = '<img src="http://graph.facebook.com/'+user.id+'/picture"><br/><a href="'+user.link+'">'+user.name+'</a>';              
            document.getElementById('data_div').innerHTML(user_data);               
        }
        else
        {
            alert("Not logged user");
            $("#login_div").show(); $("#data_div").hide();
        }
    });      
});      
</script>
<div id="login_div" style="display: none;">
    <fb:login-button data-scope="user_birthday">Login with Facebook</fb:login-button> 
</div>
<div id="data_div" style="display: none;">      
</div>

Was it helpful?

Solution

It might be that FB.init() has not finished loading and initializing the session when you call FB.api()? I ran into a similar issue. What fixed it for me was wrapping my FB.api() call in FB.getLoginStatus(), like so:

FB.getLoginStatus(function(response) {
    FB.api(function(response){ });
});

It seems to delay/make sure the session is properly initialized before calling the API. I hope it works for you too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top