FB.logout()在没有访问令牌的情况下调用

时间:2011-12-08 11:40:04

标签: javascript facebook facebook-javascript-sdk

我正在尝试退出我使用Facebook集成创建的网站。 登录工作正常,但是当我想要注销时,Firebug一直给我这个错误:

FB.logout()在没有访问令牌的情况下调用。

我正在使用Facebook JavaScript SDK,我必须注销的代码如下:

$(document).ready($(function () {
    $("#fblogout").click(facebooklogout);
}));

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

这是在Facebook Developers Documentation指定的注销代码,只是在document.ready

上分配了一个按钮。

在此代码之前,我使用FB.init()方法,一切正常。

如果有人知道为什么FB.logout没有访问令牌,那么我们将不胜感激。

6 个答案:

答案 0 :(得分:19)

要从使用facebook图谱API的应用程序注销,请在<form>标记后面的注销页面上使用此JavaScript:

window.onload=function()
{
    // initialize the library with your Facebook API key
    FB.init({ apiKey: 'b65c1efa72f570xxxxxxxxxxxxxxxxx' });

    //Fetch the status so that we can log out.
    //You must have the login status before you can logout,
    //and if you authenticated via oAuth (server side), this is necessary.
    //If you logged in via the JavaScript SDK, you can simply call FB.logout()
    //once the login status is fetched, call handleSessionResponse
    FB.getLoginStatus(handleSessionResponse);
}

//handle a session response from any of the auth related calls
function handleSessionResponse(response) {
    //if we dont have a session (which means the user has been logged out, redirect the user)
    if (!response.session) {
        window.location = "/mysite/Login.aspx";
        return;
    }

    //if we do have a non-null response.session, call FB.logout(),
    //the JS method will log the user out of Facebook and remove any authorization cookies
    FB.logout(handleSessionResponse);
}

代码正常运行并在我的网站上发布。

答案 1 :(得分:12)

我选择了不那么简单的解决方案:

    function facebookLogout(){
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                FB.logout(function(response) {
                    // this part just clears the $_SESSION var
                    // replace with your own code
                    $.post("/logout").done(function() {
                        $('#status').html('<p>Logged out.</p>');
                    });
                });
            }
        });
    }

答案 2 :(得分:3)

应该是更像这样的东西。 JS API发生了变化,您必须使用authResponse而不仅仅是session。

//handle a session response from any of the auth related calls
function handleSessionResponse(response) {

    //if we dont have a session (which means the user has been logged out, redirect the user)
    if (!response.authResponse) {
        return;
    }

    //if we do have a non-null response.session, call FB.logout(),
    //the JS method will log the user out of Facebook and remove any authorization cookies
    FB.logout(response.authResponse);
}

答案 3 :(得分:3)

我尝试过这样的事情:

function fbLogout(){
    if(typeof FB.logout == 'function'){
        if (FB.getAuthResponse()) {
         FB.logout(function(response) { window.location.href = PROJECT_PATH + '/index/logout'; }); 
         return;
        }  
    };

    window.location.href = PROJECT_PATH + '/index/logout'; 
    return;  
}

答案 4 :(得分:2)

错误表明您没有访问令牌,您必须使用FB.getAccessToken()函数检查一个。

如果没有访问令牌,则该函数返回null。见下面的例子:

   function facebooklogout() {
    try {
        if (FB.getAccessToken() != null) {
            FB.logout(function(response) {
                // user is now logged out from facebook do your post request or just redirect
                window.location.replace(href);
            });
        } else {
            // user is not logged in with facebook, maybe with something else
            window.location.replace(href);
        }
    } catch (err) {
        // any errors just logout
        window.location.replace(href);
    }
   }

答案 5 :(得分:0)

经过多次尝试后才弄清楚。 实际上,我们正在将response传递给FB.logout。

通常response.authResponse.accessToken包含令牌。因此,关于accessToken不存在的错误。

从逻辑上考虑,该响应在您的代码中来自哪里?无处不在。

因此,我们需要从函数中获取该响应对象,并将其传递给注销函数。 我不知道它对别人有什么用,但这对我有用。

只需将代码替换为此

function logout(){
  FB.getLoginStatus(function(response) {
    FB.logout(function(response){
      console.log("Logged Out!");
      window.location = "/";
    });
  });
}

我们在这里所做的是,如果用户已登录,则获取登录状态并获得相应的响应,该响应包含所有必要的令牌和数据。 然后,我们将此响应传递给注销功能。

相关问题