在FB.init()之后自动调用FB.login()

时间:2011-09-23 08:54:31

标签: facebook-graph-api login facebook-javascript-sdk

我正在为Facebook开发一款应用程序。

我的代码:

function init() {
    window.fbAsyncInit = function() {
        var appID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';

        FB.init({ appId: appID, 
              status: true, 
                  cookie: true, 
                  xfbml: true});



        login();
    };

    (function() {
        var e = document.createElement("script"); 
        e.async = true;
        e.src = "https://connect.facebook.net/en_US/all.js?xfbml=1";
        document.getElementById("fb-root").appendChild(e);
    }());
};
function login() {

    FB.login(function(response) {
        if (response.session) {
            if (response.perms) {
                // user is logged in and granted some permissions.
                // perms is a comma separated list of granted permissions
            } else {
                // user is logged in, but did not grant any permissions
            }
        } else {
            // user is not logged in
        }
    }, {perms:'read_stream,publish_stream,offline_access'});
};

我想调用“init”函数,“init”之后应该自动调用“登录”功能(打开Facebook登录窗口)。

但我总是得到“b是空的”  Firebug中出现FB.provide('',{ui:function(f,b){if(!f....onent(FB.UIServer._resultToken));}});错误。

任何人都可以帮助我吗? 有没有人有同样的问题?

由于

1 个答案:

答案 0 :(得分:1)

你不需要在一个函数中拥有facebook init东西,它可以直接在页面上,然后它会同时加载,而不是等待按钮点击加载。然后你只需要logiin按钮上的登录逻辑

<html>
<head>
</head>
<body>
<script type="text/javascript">
   window.fbAsyncInit = function() {
        var appID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
        FB.init({
              appId: appID, 
              status: true, 
              cookie: true, 
              xfbml: true
        });

        // This bit adds the login functionality to the login
        // button when api load is complete
        document.getElementById("login").onclick = function() {
        FB.login(function(response) {
            if (response.session) {
                if (response.perms) {
                    // user is logged in and granted some permissions.
                    // perms is a comma separated list of granted permissions
                } else {
                    // user is logged in, but did not grant any permissions
                }
            } else {
                // user is not logged in
            }
        }, {perms:'read_stream,publish_stream,offline_access'});
    }
    };
    (function() {
        var e = document.createElement("script"); 
        e.async = true;
        e.src = "https://connect.facebook.net/en_US/all.js?xfbml=1";
        document.getElementById("fb-root").appendChild(e);
    }());
</script>
<a id="login" href="somewhere.html">My login button</a>
</body>
</html>

您可以将登录内容放在方法中并在FB.init之后自动调用它,但大多数浏览器会阻止弹出窗口。您最好等待用户单击登录按钮以确保他们正确地看到它,并且通常只有在用户明确请求时才会使事情发生。我认为facebook的“良好做法”指南也在某处提到了这一点。

相关问题