FB JS-SDK无法直接检测用户从FB注销

时间:2011-10-22 04:34:50

标签: javascript facebook facebook-javascript-sdk

TL; DR Facebook javascript SDK无法确定直接从facebook.com下面注销的用户的准确登录状态。

我正在开发一个使用javascript SDK进行身份验证的Facebook应用。我注意到,当用户直接退出Facebook(在facebook.com上)时,SDK无法理解用户在应用页面上的登录状态。我把它归结为以下简单的测试用例。

在一个标签中登录facebook.com。在另一个选项卡中,渲染下面的应用页面(根据需要替换MY_APP_ID)。如果您以从未授予此应用程序权限的用户身份登录Facebook,请单击“登录”并授予他们。无论哪种方式,您都应该看到一些事件触发,表明您已登录。

现在,回到facebook.com标签,退出facebook。转到应用程序选项卡,然后单击所有按钮(“登录”除外)并查看浏览器控制台输出。

  • 调用FB.logout时,没有任何反应。永远不会调用回调。在Chrome中,出现以下错误:“不安全的JavaScript尝试从包含网址http://my_app.com的网址访问包含网址http://www.facebook.com/的框架。域名,协议和端口必须匹配。”

  • 调用FB.getLoginStatus时,响应显示“已连接”并具有authResponse对象。但这没有用,因为它现在不正确。相反,如果为“force”参数传入true,则不会发生任何事情(永远不会调用回调)。

  • 调用FB.getAuthResponse时,没有任何反应(永远不会调用回调)。

  • 在任何此类事件中(在退出Facebook之后)都没有触发任何相关事件。

问题在于,在这种情况下似乎没有可能通过SDK合法地确定用户的登录状态。或者我做了一些愚蠢的事情,尽管我把它煮到了最低限度。

我的最终目标是,如果用户在退出Facebook后点击我的退出按钮,我希望能够在这种情况下执行某些事情。但SDK没有提供实现这一目标的可行方法。

任何想法或想法?有没有其他人经历过这个并找到了解决方法?谢谢!

简单的应用页面:

<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml' xmlns:fb='http://www.facebook.com/2008/fbml'>
<head>
</head>
<body>
<script>
  window.fbAsyncInit = function() {
    function subFBEvent(name) {
      FB.Event.subscribe(name, function(r) {
        console.log(name);
        console.log(r);
      });
    }
    subFBEvent('auth.login');
    subFBEvent('auth.logout');
    subFBEvent('auth.authResponseChange');
    subFBEvent('auth.statusChange');
    FB.init({
      appId  : 'MY_APP_ID',
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true,  // parse XFBML
      oauth  : true       // use OAuth 2.0
    });
  };
  (function() {
    var s = document.createElement('div'); 
    s.setAttribute('id','fb-root'); 
    document.documentElement.getElementsByTagName("body")[0].appendChild(s);
    var e = document.createElement('script');
    e.src = 'http://connect.facebook.net/en_US/all.js';
    e.async = true;
    s.appendChild(e);
  }());
</script>
<button onclick="FB.logout(function(r) { console.log('FB.logout'); console.log(r); });">Logout</button>
<button onclick="FB.getLoginStatus(function(r) { console.log('FB.getLoginStatus'); console.log(r); });">LoginStatus</button>
<button onclick="FB.getAuthResponse(function(r) { console.log('FB.getAuthResponse'); console.log(r); });">AuthResponse</button>
<fb:login-button>Login</fb:login-button>
</body>

2 个答案:

答案 0 :(得分:0)

我不认为这个解决方案现在会对你有所帮助,但我发布它是为了让其他人不会有这么难的时间。 这是因为您没有正确实施FB.getLoginStatus方法。 你所要做的就是实现这个方法

 FB.getLoginStatus(function(response) {
    statusChangeCallback(response);
  });
初始化调用后

。此方法将调用statusChangeCallback方法并进行身份验证。

function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      testAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into Facebook.';
    }
  }

有关更多参考,请参阅官方文档的此页面 https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.0

答案 1 :(得分:-2)

首先:为什么你想要做到这一点?

Facebook登录基本上只是方便Facebook用户避免在您的网页上填写注册或登录表单。它允许在您自己的网站上轻松设置用户会话,但据我所知,它不允许您直接在Facebook的会话中登录用户(也不允许任何其他启用Facebook登录的网站,如Stackoverflow )。这反过来也是如此:如果用户退出Facebook并不意味着他应该失去通过Facebook登录建立的所有其他会话。

相关问题