<...">

FaceBook login button error: FB.login () is called before calling FB.init ()

I get this error after executing this code:

 <div id="fb-root"></div>
 <script src="http://connect.facebook.net/en_US/all.js"></script>
 <script>
   FB.init({
     appId  : 'realIdInTheCode',
     status : true, // check login status
     cookie : true, // enable cookies to allow the server to access the session
     xfbml  : true  // parse XFBML
   });
 </script>

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=realIdInTheCode&amp;xfbml=1">
</script><fb:login-button show-faces="false" width="200" max-rows="1"></fb:login-button>

In my opinion, you are calling init () before login ()

But I still get this error: Error: FB.login () is called before calling FB.init ().

At the moment, I am completely confused, so I am very grateful for the help :)

+3
source share
3 answers

For me, this turned out to be a problem with the application itself. Therefore, I deleted the old application in FB and made a new one, and everything just started working.

+1
source

I used to think that FBML was deprecated. I just tested, it works for me

<html>
    <head>
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
          //initializing API
          window.fbAsyncInit = function() {
            FB.init({appId: 'MYAPPID', status: true, cookie: true,
                     xfbml: true});
          };
          (function() {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol +
              '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
          }());
        </script>

        <button type="button" onclick="fblogin();">Facebook Login</button>


        <script>
          //your fb login function
          function fblogin() {
            FB.login(function(response) {
                //User logged in!
                if (response.session) {
                    alert('YAY!');
                } else {
                // user cancelled login
                    alert('NAY!');
                }

            }, {perms:'email,read_stream,publish_stream'});
          }
        </script>

    </body>
</html>

OR

    <html>
        <head>
        </head>
        <body>
            <script src="http://connect.facebook.net/en_US/all.js"></script>
            <script>
              FB.init({
                appId  : 'MYAPPID',
                status : true, // check login status
                cookie : true, // enable cookies to allow the server to access the session
                xfbml  : true  // parse XFBML
              });

            </script>
        <button type="button" onclick="fblogin();">Facebook Login</button>
        <script>
          //your fb login function
          function fblogin() {
              FB.login(function(response) {
                  //User logged in!
                  if (response.session) {
                      alert('YAY!');
                  } else {
                  // user cancelled login
                      alert('NAY!');
                  }

              }, {perms:'email,read_stream,publish_stream'});
            }
        </script>

    </body>
</html>

cm

+4

Try this code:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : 'realIdInTheCode',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
  });
</script>
<fb:login-button show-faces="false" width="200" max-rows="1"></fb:login-button>
+2
source

Source: https://habr.com/ru/post/1790230/


All Articles