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

时间:2021-09-17 19:43:57

I'm trying to log out of a website i've created with Facebook integrated. Logging in works fine, but when I want to log out Firebug consistently gives me this error:

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

FB.logout() called without an access token.

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

I'm using the Facebook JavaScript SDK, and the code I've got to logout looks like this:

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

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

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

This is the logout code specified at the Facebook Developers Documentation just with a button being assigned the method on document.ready

这是在Facebook开发人员文档中指定的注销代码,只是在document.ready上分配了一个按钮

Before this code I have the FB.init() method, that all runs fine.

在此代码之前我有FB.init()方法,所有运行正常。

If anyone's got a solution as to why FB.logout doesn't have an access token, it'd be appreciated.

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

5 个解决方案

#1


17  

To logout from the application which uses facebook graph API, use this JavaScript on the logout page just after the <form> tag:

要从使用facebook图谱API的应用程序注销,请在

标记后面的注销页面上使用此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);
}

The code works and is live on my site.

代码可以在我的网站上运行。

#2


11  

I went for the less trivial solution:

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

    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>');
                    });
                });
            }
        });
    }

#3


4  

Should be something more like this. There was a change to the JS API where you have to use authResponse instead of just session.

应该是更像这样的东西。 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);
}

#4


3  

I've tried something like this:

我尝试过这样的事情:

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

#5


2  

The error says that you don't have an access token, you have to check for one using the FB.getAccessToken() function.

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

If there is no access token the function returns null. See example below:

如果没有访问令牌,则该函数返回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);
    }
   }

#1


17  

To logout from the application which uses facebook graph API, use this JavaScript on the logout page just after the <form> tag:

要从使用facebook图谱API的应用程序注销,请在

标记后面的注销页面上使用此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);
}

The code works and is live on my site.

代码可以在我的网站上运行。

#2


11  

I went for the less trivial solution:

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

    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>');
                    });
                });
            }
        });
    }

#3


4  

Should be something more like this. There was a change to the JS API where you have to use authResponse instead of just session.

应该是更像这样的东西。 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);
}

#4


3  

I've tried something like this:

我尝试过这样的事情:

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

#5


2  

The error says that you don't have an access token, you have to check for one using the FB.getAccessToken() function.

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

If there is no access token the function returns null. See example below:

如果没有访问令牌,则该函数返回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);
    }
   }