Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook javascript sdk An active access token must be used to query information about the current user

In my code I have

FB.api('/me/friends', function(response) {
            if(response.data) {
                //TODO : what to do if no. of friends is more than 5000 (pagination by fb)
                friends_data=response.data;
                dijit.registry.byId("mainWidget_div").set_friends_data(friends_data);
            } else {
                alert("Error!");
            }

          });

And this gives an error. But, if I call this function manually(on the console), there's no error

FB.api('/me/friends', function(response){r=response;});
//wait a while
r

and now r.data is an array of my friends.

I checked the network panel and I gather that when I call this manually, an access token automatically gets inserted in the request url and when it is getting called via the code, the access token doesn't get inserted.

The full fb sdk loading code in my application is this:

<script type="text/javascript">
      // You probably don't want to use globals, but this is just example code
      var fbAppId = "{{facebook_app_id}}";

      // This is boilerplate code that is used to initialize the Facebook
      // JS SDK.  You would normally set your App ID in this code.

      // Additional JS functions here
      window.fbAsyncInit = function() {
        FB.init({
          appId      : fbAppId,        // App ID
          status     : true,           // check login status
          cookie     : true,           // enable cookies to allow the server to access the session
          xfbml      : true            // parse page for xfbml or html5 social plugins like login button below
        });

        // Put additional init code here
        dojo.ready(function(){
          FB.api('/me/friends', function(response) {
            if(response.data) {
                //TODO : what to do if no. of friends is more than 5000 (pagination by fb)
                friends_data=response.data;
                dijit.registry.byId("mainWidget_div").set_friends_data(friends_data);
            } else {
                alert("Error!");
            }

          });
        });
      };
      // Load the SDK Asynchronously
      (function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/en_US/all.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));

    </script>
like image 269
prongs Avatar asked Apr 04 '13 13:04

prongs


1 Answers

The answer from Brent Baisley and another answer to a different question, helped me figure out what was wrong.

You can't call FB.init() dependent methods right after FB.init() because it loads asynchronously. Even loading the data asynchronously like in dojo.ready() doesn't help. You have to wrap the code in FB.getLoginStatus().

like image 188
Eva Avatar answered Sep 27 '22 19:09

Eva