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

后端 未结 6 931
北恋
北恋 2020-12-08 04:18

I\'ve this message in my console

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

My code



        
相关标签:
6条回答
  • 2020-12-08 04:43

    I got it working using this code:

    <!--[if IE]>
    <script src="https://connect.facebook.net/pt_BR/all.js?xfbml=1" type="text/javascript"></script>
    <![endif]-->
    <script src="https://connect.facebook.net/pt_BR/all.js#xfbml=1" type="text/javascript"></script>
    
     <div class="fb-like"
              data-href="https://www.myurl.com" 
              data-send="true" data-layout="button_count" 
              data-width="50" data-show-faces="false"></div>
    
    0 讨论(0)
  • 2020-12-08 04:45

    Warning: some speculation below. If that's not OK, I can delete it.

    I'd assume that the problem lies in loading the SDK asynchronously but the Like button inline. Then it's possible that the Like button calls getLoginStatus before the script has loaded. You can test/verify this by commenting out the script to load the SDK asynchronously and instead include it as a normal script tag in the page head.

    Of course, that's just the easy way to test. If you can verify that as the problem, the "proper" way to solve it would probably be to ensure that the Like button doesn't appear on the page until the SDK has loaded.

    0 讨论(0)
  • 2020-12-08 04:47

    Short answer

    Add &status=0 to your js.src URL to make the warning disappear, ie:

    //connect.facebook.net/fr_FR/all.js#xfbml=1&status=0

    Full answer

    FB.init() will be called internally by the facebook script upon loading if parameters are provided after the hash (#) sign. Here xfbml is passed, so FB.init() is called.

    (source code: http://connect.facebook.net/fr_CA/all/debug.js line 8699 at the time of this post)

    The default args for init() are used if not explicitly provided: status arg default is true - which makes the FB script call getLoginStatus() at startup, which complains because an app ID is needed for that function call.

    FB social plugins doesn't need an app ID - they are rendered into an iframe originating from facebook.com, so FB login status and cookies as accessible to them.

    The "Get the Code" wizards in the FB developers Social Plug-ins section generates a URL with the xfbml param, it should be updated with the status=0 param IMHO.

    0 讨论(0)
  • 2020-12-08 04:53

    You need to call FB.init function before the calling fb login status function

        <div id="fb-root"></div>
        <script src="//connect.facebook.net/en_US/all.js"></script> 
        <script>
          FB.init({
                appId  : 'YOUR APP ID',
                status : true, // check login status
                cookie : true, // enable cookies to allow the server to access the session
                xfbml  : true,  // parse XFBML
                oauth : true
            });
    
        // Load the SDK Asynchronously
        (function(d){
           var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
           if (d.getElementById(id)) {return;}
           js = d.createElement('script'); js.id = id; js.async = true;
           js.src = \"//connect.facebook.net/fr_FR/all.js#xfbml=1\";
           ref.parentNode.insertBefore(js, ref);
         }(document));
      </script>
    
    0 讨论(0)
  • 2020-12-08 04:57

    If you see the warning of 'Invalid App ID' after implementing solution of

    https://stackoverflow.com/a/16593474/1428052

    then add appId to your js.src URL to make it work for you, i.e.:

    //connect.facebook.net/en_US/all.js#xfbml=1&status=0&appId=YOUR_APP_ID

    The presence of this appId, will automatically call FB.init() with basic app settings.

    0 讨论(0)
  • 2020-12-08 04:59

    I answered to a similar SO question here.
    Actually, if you also cannot see the Like button at all, then this question is an exact duplicate of the other one.

    In my case, I'm seeing the console error, but I do see the Like button as well.

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