Facebook登录按钮错误:在调用FB.init()之前调用了FB.login()。

3

在执行这段代码后,我遇到了以下错误:

 <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>

我认为问题出在在调用login()函数之前没有调用init()函数。

但是我仍然遇到了这个错误:Error: 在调用FB.init()之前调用了FB.login()。

此时我有点困惑,所以真的很感激您的帮助 :)


为什么你有两个“fb-root” div元素和两个Facebook JavaScript脚本元素? - Pavel Surmenok
很可能是因为我有点迷失在我正在做的事情和正确的处理方式上 :) - Genadinik
3个回答

4

我曾经认为FBML已经被弃用了,但是我刚刚测试了一下,它对我起作用了。

<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>

或者

    <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>

refer:


我为此制作了一个测试页面:http://www.comehike.com/test_fb_connect.php -- 试一下吧...不起作用 :( - Genadinik
@Genadinik 可能错用了 APPID,请参考这里的链接 https://dev59.com/E3E85IYBdhLWcg3wShaf - Nishant
@Genadinik 嗯,可能不行。你能试着删除所有与Facebook相关的JS和<FB:..标签吗?不要只是注释掉它们,而是彻底删除。除了脚本中的那个之外。我刚刚测试了所有提到的三种方法作为答案...它们都有效! - Nishant
我三次确认了我正在使用正确的应用程序ID。但仍然没有任何进展。:( - Genadinik

2
尝试这段代码:

试试这段代码:

<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>

尝试了一下,但是不起作用。我怀疑这可能与ComeHike.com网站上的其他内容有关。不确定是否有人能发现那里有什么异常吗? - Genadinik

1

对我来说,这似乎是应用本身的问题。所以我在 Facebook 上删除了旧应用并创建了一个新的,然后一切都开始正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接