【问题标题】:FB.logout() called without an access tokenFB.logout() 在没有访问令牌的情况下调用
【发布时间】:2012-01-15 20:37:05
【问题描述】:

我正在尝试退出我创建的集成了 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 没有访问令牌的原因有解决方案,我们将不胜感激。

【问题讨论】:

    标签: javascript facebook facebook-javascript-sdk


    【解决方案1】:

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

    代码有效并且在我的网站上有效。

    【讨论】:

    • 感谢 Shadow 格式化我的代码。请注意,您需要将此 HTML 放在
      标记之后和 javascript -
      之前
    • Bill 我只是想知道您的解决方案是否会创建某种循环,因为方法 handleSessionResponse 是从自身内部调用的。 (只是为了让您了解技能和理解,我是一名初级开发人员,只学习了 6 个月。)
    • 是的,马丁。你是绝对正确的。该函数是递归的,并且在会话返回空值之前一直被调用。递归方法背后的想法是,它应该继续向 facebook 发送“注销”请求,直到 facebook 上的用户会话实际被破坏;因为我的测试表明向 facebook 发出单个“注销”请求并非每次都有效(可能是 fb api 错误)。上面的代码只是为了确保用户注销。如果您可以编写一个非递归方法重用上述代码并将其发布为 SO 用户参考的替代方法,我将不胜感激
    • 巧妙的把戏。通过执行相同类型的操作(循环调用 FB.logout() 直到会话真正被破坏),我能够为我自己的应用程序采用此代码
    • 棘手的事情。谢谢。在尝试您的解决方案之前遇到同样的问题。
    【解决方案2】:

    我选择了更简单的解决方案:

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

    【讨论】:

    • 您的代码如果错误,如果用户没有与 facebook 连接,您仍然希望将他与您的网站断开连接(例如 post('/logout')
    • @dorian 谢谢,但这是我自己的代码,不,没有错。我在上面清楚地写了“用你自己的代码替换”。附:如果没有看到在mydomain.com/logout url 中执行的代码,你就无法做出判断
    • 我的意思是,如果有人点击“注销”按钮,即使他们没有登录到 Facebook,你也想清除他们的会话:gist.github.com/Dorian/f65a976f6b08739dcf83
    • @Dorian 哦,好吧,反正我只是在清除会话(没有人喜欢 fb 注销功能将他们从 facebook 以及应用程序中注销)
    • @Dorian 如果他们隐式登录,即他们从应用程序的登录流程登录到 Facebook,而不是从 facebook.com 登录,它只会将他们从 facebook 中注销
    【解决方案3】:

    经过多次尝试,终于搞定了。

    一般response.authResponse.accessToken 包含令牌。所以,关于 accessToken 不存在的错误。

    从逻辑上思考,您的代码中的响应来自哪里?不知从何而来。

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

    只需用这个替换代码

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

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

    【讨论】:

    • 在调用 FB.logout 之前,我还检查了 response.authResponse 中是否有任何东西
    • 是的!这增加了另一层预防措施。
    • 如何“将响应传递给注销功能”? FB.logout 的回调函数参数中的响应应该在注销后由 FB API 传递...
    【解决方案4】:

    我尝试过这样的事情:

    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】:

      应该更像这样。 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.logout() 就足够了。什么会调用handleSessionResponse 函数呢?这是什么神秘的response?上面的答案澄清了这一点。
      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签