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

前端 未结 2 464
野性不改
野性不改 2020-12-14 14:21

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().

<
相关标签:
2条回答
  • 2020-12-14 15:04

    This error will also occur if you don't provide an appId to FB.init. Example:

        FB.init({
            appId: ''
            , channelUrl: '//domain/channel.html'
            , status: true
            , cookie: true
        });
    

    Will result in:

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

    0 讨论(0)
  • 2020-12-14 15:10

    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!

    0 讨论(0)
提交回复
热议问题