Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

javascript - FB.getLoginStatus() called before calling FB.init() error in console

I am trying to check if a user is logged in with my app, but I am getting a

FB.getLoginStatus() called before calling FB.init().

error in the console. The setSize appears to work (although not entirely 1,710 height but definitely around 1,500) so I cannot understand why the getLoginStatus() gives the error.

I also double checked with the appID (removed below) and that is definitely correct. The script is included via <script src="file.js" type="text/javascript"></script> right below my <body> and <div id="fb-root"></div> div.

window.fbAsyncInit = function() {
FB.init({
    appId      : 'APPID', // 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.Canvas.setSize({width: 520, height: 1710});

FB.Canvas.setAutoResize(100);

FB.getLoginStatus(function(response) {
    if (response.authResponse) {
        // logged in and connected user, someone you know
        alert("?");
    } else {
        // no user session available, someone you dont know
        alert("asd");
    }
});
};
question from:https://stackoverflow.com/questions/8048564/fb-getloginstatus-called-before-calling-fb-init-error-in-console

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to load the api asynchronously. Try removing your <script src="connect.facebook.net/en_US/all.js"></script> and updating your JS to:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'YOUR_APP_ID', // App ID
      channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
      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
    });

    //
    // All your canvas and getLogin stuff here
    //
  };

  // 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 = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>

Oh and check the documentation!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...