遇到Facebook JS SDK的问题

时间:2014-08-27 15:40:17

标签: javascript jquery facebook sdk

大约一半的时间有效,另一半没有。通过" work",我的意思是人们声称只是"退出" Facebook并没有发生任何事情 - 也就是说,他们很可能被重定向回我的/index页面。

我的目标非常简单:如果用户登录或退出Facebook,请将其注销并为他们提供使用其凭据登录的选项。假设他们成功登录,我执行_login函数,在那里我将它们重定向到我的/auth/fb/<verification code>/处理程序。此处理程序将新用户添加到我的数据库。

我不想&#34;假设&#34;当他们执行此注册时,他们登录或退出Facebook,所以就像我上面所说,我需要他们与FB JS auth对话。

我很擅长使用Javascript,更不用说使用FB SDK了,所以请耐心等待我,这样我就可以改进了。谢谢。

我使用api的完整JS代码是:

// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
    if (response.status === 'connected') {
        // Logged into Facebook
    } else if (response.status === 'not_authorized') {
        //console.log("not auth");
    } else {
        // The person is not logged into Facebook, so we're not sure if
        // they are logged into this app or not.
    }
}

// This function is called when someone finishes with the Login
// Button.  See the onlogin handler attached to it in the sample
// code below.
function checkLoginState() {
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });
}

window.fbAsyncInit = function() {
    FB.init({
        appId      : '<my_appid>',
        cookie     : true,  // enable cookies to allow the server to access the session
        xfbml      : true,  // parse social plugins on this page
        oauth      : true,
        version    : 'v2.0' // use version 2.0
    });

    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });

    button = document.getElementById('fb-auth');
    button.onclick = function(response) {

        FB.login(function(response) {
            // Handle the response object, like in statusChangeCallback() in our demo code.
            FB.api('/me', function(info) {
                _login();
            });
        });
    };
};

// Load the SDK asynchronously
(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];

    if (d.getElementById(id)) return;

    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=<my_appid>&version=v2.0";
    fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));

// Here we run a very simple test of the Graph API after login is
// successful.  See statusChangeCallback() for when this call is made.
function _login() {
    window.location = "/auth/login/fb/" + _getVerificationCode() + "/";
}

function _logout(response) {
    FB.logout(function(response){});
}

function _getVerificationCode() {
    var vc = document.getElementById('verificationCode');
    if (vc !== null) {
        return vc.value;
    } else {
        return null;
    }
}

0 个答案:

没有答案