没有访问令牌而调用FB.logout()

FB.logout() called without an access token

本文关键字:logout FB 调用 访问令牌      更新时间:2023-09-26

我想退出一个网站,我已经与Facebook集成创建。登录工作正常,但当我想注销Firebug总是给我这个错误:

没有访问令牌的FB.logout()被调用。

我使用的是Facebook JavaScript SDK,我要注销的代码看起来像这样:

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

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

上的方法

在此代码之前,我有FB.init()方法,这一切都运行良好。

如果有人有一个解决方案,为什么FB。注销没有访问令牌,非常感谢。

要从使用facebook graph 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);
}

代码工作,并在我的网站上。

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

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

试了这么多次才明白。

一般response.authResponse.accessToken包含token。所以,它的错误是关于accessToken不在那里。

从逻辑上思考,这个响应在你的代码中来自哪里?不知从哪里冒出来的

我们需要从一个函数中获取那个响应对象并让它工作。我不知道别人是怎么做的,但这对我有用。

只需将代码替换为

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

我们在这里所做的是,如果用户已登录,则获取登录状态,并获得相应的响应作为返回,其中包含所有必要的令牌和数据。一旦获取到这个,就使用令牌注销用户。

我试过这样做:

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

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

错误说明您没有访问令牌,您必须使用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);
    }
   }

在Typescript中,这个函数可以很好地工作。

  signOutFacebook(): void {
    /*SIGN OUT USER FACEBOOK.*/
    FB.getLoginStatus(function (response) {
      if (response.status === 'connected') {
        FB.logout(function (response) {
          console.log("Logged Out!");
        });
      } else {
        console.log("The person is not logged into your webpage or we are unable to tell. !");
      }
    });
  }/*FINAL 'signOutFacebook()'. */

与FB一起使用访问令牌。方法注销时,您需要确保在登录过程中获得了访问令牌,并且在您想要注销用户时它可用。

FB.logout(function(response) {
                
    if ( 'connected' === response.status ) {
        window.location.href = '/';
    } else {
        console.log( 'There has been an error.' );
    }
}, {
    access_token: accessToken
});