简体   繁体   中英

Facebook session always null after page refresh - javascript

I am using javascript in my webpage to retrieve the session for a user who has already approved my app and who is connected to Facebook. However, when I refresh the page after logging into Facebook the session is never retrieved and is always null... I can't understand why.

here is my javascript for facebook connect:

          window.fbAsyncInit = function() {

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

        FB.getLoginStatus(function(response) {
            if (response.session) {
            showLogout();
           } else {
            showLogin();
           }
        });             
      };
      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());

         FB.Event.subscribe('auth.login', function() {
         showLogout();
       });

    //when a user hits login it calls this function which works fine.
    function loginfuncfb() {
    FB.login(function(response) {
     if (response.session) {
       if (response.perms) {
       showLogout();
       } else {
       showLogin();
      }
       } else {n
       showLogin();
       }
    }, {perms:'email,publish_stream,offline_access'});
    }

Please help. Thanks

*EDIT

I just want to let out some frustration here and tell you all that I have never in my life worked with such a problematic set of developer tools/SDKs as that of Facebook and I have never found it so difficult to get answers regarding technical difficulties. There are hardly any real solutions posted regarding facebook issues, the Facebook documentation is pathetic, and worst of all the terminology used in referring to facebook functions etc is really vague. I can't believe how long i've been stuck on this stupid petty problem...

好了,在阳光下尽一切努力解决了这个问题之后,事实证明,我接受Firefox的3rd cookie是残疾人。

Just a wild guess without looking at any documentation, maybe the order you are doing things is wrong --- it seems like

  (function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
  }());

Should be done before anything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM