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

mar*_*rdy 33 javascript facebook facebook-javascript-sdk

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

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

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

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

function facebooklogout() {
    FB.logout(function (response) {
    }
)};
Run Code Online (Sandbox Code Playgroud)

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

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

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

Bal*_*dar 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);
}
Run Code Online (Sandbox Code Playgroud)

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

  • 是的马丁.你是绝对正确的.该函数是递归的,并且在会话返回空值之前被调用.递归方法背后的想法是它应该继续向facebook发送'注销'请求,直到facebook上的用户会话被实际销毁为止; 因为我的测试表明,每次向facebook发送一个"注销"请求都不起作用(可能是一个fb api bug).上面的代码只是为了确保用户注销.如果您可以编写一个非递归方法重用上述代码并将其作为SO用户参考的替代方法发布,我将不胜感激 (3认同)

Ozz*_*zzy 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>');
                    });
                });
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

  • @dorian谢谢,但这是我自己的代码,不,这没有错.我清楚地在它上面写了"替换为你自己的代码".如果没有看到在`mydomain.com/logout`网址中执行的代码,你无法做出判断 (2认同)

Sum*_*ara 7

试了好多次才搞定。

一般response.authResponse.accessToken包含token。因此,它关于 accessToken 不存在的错误。

合乎逻辑地思考,该响应来自您的代码中的哪里?从哪儿冒出来。

因此,我们需要从函数中获取该响应对象并使其正常工作。我不知道它对其他人如何,但这对我有用。

只需用这个替换代码

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

我们在这里做的是,如果用户已登录,则获取登录状态并返回相应的响应,其中包含所有必要的令牌和数据。获取此信息后,该令牌将用于注销用户。