Facebook图形API:在调用FB.init()之前调用了FB.login()

25

我正在尝试在我的网站上使用最新的Facebook Graph API。这是我所拥有的:

在页面的某处:

<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>

在 <head> 标签之后:

<div id="fb-root">
    </div>
    <script type="text/javascript">
        window.fbAsyncInit = function () {
            FB.init({ appId: '<%= ConfigurationManager.AppSettings["FBAppId"] %>', status: true, cookie: true, xfbml: true });

            /* All the events registered */
            FB.Event.subscribe('auth.login', function (response) {
                // do something with response
                alert("login success");
            });
            FB.Event.subscribe('auth.logout', function (response) {
                // do something with response
                alert("logout success");
            });

            FB.getLoginStatus(function (response) {
                if (response.session) {
                    // logged in and connected user, someone you know
                    alert("login success");
                }
            });
        };
        (function () {
            var e = document.createElement('script');
            e.type = 'text/javascript';
            e.src = document.location.protocol +
            '//connect.facebook.net/en_US/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        } ());
    </script>

但是当我点击生成的登录按钮时,没有任何反应。

此外,我还遇到了

FB.getLoginStatus() called before calling FB.init().
在 Firebug 控制台中。有任何想法?
3个回答

45

我简直不敢相信,原来是因为在Web.config中引用了一个不存在的密钥,导致FB.init默默失败。

现在它按预期工作了。

更明确地说,我没有将appId传递给FB.init,一旦传递了,它就可以正常工作了。


2
这意味着,我没有将一个键传递给FB.init方法。 - sabbour
我遇到了同样的问题,但只在MAC OSX上的FF浏览器中出现,其他浏览器都可以正常工作。请帮助我解决这个问题。请查看我的问题:http://stackoverflow.com/questions/3338198/facebook-login-plugin-issue-with-ff-on-mac-osx - Nnp
1
你能否请发一下完整的可运行代码?我这里也遇到了同样的问题。谢谢。 - Intellix
1
在我的情况下,FB.init中缺少了appId。我认为这也是这里的情况。 - Tomasz Struczyński
2
很高兴知道我被 Facebook API 中已知的一个 bug 搞了两年... - Glenn Maynard

3

我曾经遇到过同样的问题,以下是对我有效的解决方案。只需将以下内容放在您的头部区域中,或者换句话说,将app_id添加到Facebook js的源代码中。

<script src="//connect.facebook.net/zh_CN/all.js&appId=xxxxxxxxxxxxxxx" type="text/javascript"></script>


2
在我的情况下,问题在我禁用了XFBML功能后消失了(即将xfbml键设置为false,移除fb:命名空间和<script src="…">中的#xfbml=1片段)。好在我也不需要它。
你的情况可能有所不同,此错误消息出现的其他原因也是有效的。

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