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

Facebook graph api 在FB.init()之后自动调用FB.login(),facebook-graph-api,login,facebook-javascript-sdk,Facebook Graph Api,Login,Facebook Javascript Sdk,我正在为Facebook开发一个应用程序 我的代码: function init() { window.fbAsyncInit = function() { var appID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'; FB.init({ appId: appID, status: true, cookie: true, xfb

我正在为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”之后应该自动调用“login”函数(打开Facebook登录窗口)

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

有人能帮我吗? 有人有同样的问题吗


谢谢

您不需要在函数中包含facebook init内容,它可以直接在页面上运行,然后它将同时加载,而不是等待单击按钮加载。然后您只需要登录按钮上的登录逻辑

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

window.fbAsyninit=函数(){
变量appID='XXXXXXXXXXXXXXXXXXXXXX';
FB.init({
appId:appId,
状态:正确,
曲奇:是的,
xfbml:对
});
//此位将登录功能添加到登录
//api加载完成时的按钮
document.getElementById(“login”).onclick=function(){
FB.登录(功能(响应){
if(response.session){
if(response.perms){
//用户已登录并被授予某些权限。
//perms是以逗号分隔的已授予权限列表
}否则{
//用户已登录,但未授予任何权限
}
}否则{
//用户未登录
}
},{perms:'read_stream,publish_stream,offline_access'});
}
};
(功能(){
var e=document.createElement(“脚本”);
e、 异步=真;
e、 src=”https://connect.facebook.net/en_US/all.js?xfbml=1";
document.getElementById(“fb根”).appendChild(e);
}());

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

我的HTML页面上有一个按钮“启动facebook”。这个按钮称为“初始化”功能。当我使用两个按钮(一个调用init,另一个调用login)时,一切都很好,但我只需要一个按钮。Thx,但我必须将“init”放在函数中。是否可以在初始化后自动加载登录?是的,但是所有用户的浏览器都会阻止弹出窗口,通常在用户上强制弹出窗口是个坏主意。我不会强制弹出窗口:)我想要一个按钮。如果用户点击这个按钮=>init Facebook Connection并打开登录窗口。啊,我明白了。你为什么不想事先开始呢?当用户单击按钮时加载api可能需要时间并延迟响应,这可能会让用户感到恼火。我试过那个代码,它似乎对我来说还可以。你有一个活的例子吗?我从一个xml文件加载代码,并在运行时集成功能。你知道如何在FB.init之后自动加载FB.login吗?