FB JS-SDK无法检测到直接从FB注销的用户 [英] FB JS-SDK cannot detect user logging out of FB directly

查看:68
本文介绍了FB JS-SDK无法检测到直接从FB注销的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在使用javascript SDK进行身份验证开发Facebook应用.我注意到,当用户直接从Facebook(在facebook.com上)注销时,SDK无法理解应用程序页面上用户的登录状态.我将其简化为以下简单的测试用例.

I'm developing a Facebook app using the javascript SDK for authentication. I've noticed that, when the user logs out of Facebook directly (on facebook.com), then the SDK cannot make sense of the user's login state on the app page. I've boiled it down the the following simple test case.

在一个标签中登录facebook.com.在另一个标签中,呈现下面的应用页面(适当替换MY_APP_ID).如果您以从未授予此应用程序权限的用户身份登录到Facebook,请单击登录"并授予他们.无论哪种方式,您都应该看到一些事件触发,表明您已登录.

Log in to facebook.com in one tab. In another tab, render the app page below (replacing MY_APP_ID as appropriate). If you are logged into facebook as a user that has never given permissions to this app, click "Login" and grant them. Either way, you should see some events fire indicating you are logged in.

现在,返回facebook.com选项卡,注销Facebook.转到应用程序选项卡,然后单击所有按钮(登录"除外),然后查看浏览器控制台的输出.

Now, back on the facebook.com tab, log out of facebook. Go to the app tab and click all the buttons (except "Login") and look at the browser console output.

  • 调用FB.logout时,没有任何反应.永远不会调用该回调.在Chrome中,出现以下错误:不安全的JavaScript尝试从URL为 http://my_app.com 的框架="http://www.facebook.com/"> http://www.facebook.com/.域,协议和端口必须匹配."

  • When FB.logout is called, nothing happens. The callback is never called. In Chrome, the following error is given: "Unsafe JavaScript attempt to access frame with URL http://my_app.com from frame with URL http://www.facebook.com/. Domains, protocols and ports must match."

调用FB.getLoginStatus时,响应说已连接"并具有authResponse对象.但是,这没有帮助,因为它现在不正确.相反,如果为"force"参数传递了true,则什么也不会发生(永远不会调用该回调).

When FB.getLoginStatus is called, the response says "connected" and has an authResponse object. This is not helpful, though, because it's now incorrect. If, instead, true is passed in for the "force" parameter, then nothing happens (the callback is never called).

调用FB.getAuthResponse时,什么也没有发生(永远不会调用该回调).

When FB.getAuthResponse is called, nothing happens (the callback is never called).

在任何一个过程中(退出Facebook之后)都不会触发任何相关事件.

None of the relevant events are fired during any of this (after logging out of facebook that is).

然后的问题是,在这种情况下,似乎没有办法从SDK合法地确定用户的登录状态.或者我正在做一些愚蠢的事情,尽管我把它煮到最低限度了.

The issue then is that there seems to be no possible way to legitimately determine the user's login status from the SDK in such a case. Or I'm doing something stupid, though I've boiled it down to the bare minimum.

在这里我的最终目标是,如果用户在已经注销Facebook之后单击我的注销按钮,那么我希望能够在这种情况下执行 操作.但是SDK没有提供实现此目标的可行方法.

My end goal here is that I want to be able to do something in this case if the user clicks my logout button after already logging out of facebook. But the SDK gives no viable means of achieving this.

有什么想法或想法吗?其他人有没有遇到过这种情况并找到了解决方法?谢谢!

Any ideas or thoughts? Has anyone else experienced this and found a workaround? Thanks!

简单的应用页面:

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

推荐答案

我认为该解决方案现在不会为您提供帮助,但是我正在发布它,以便其他人不会遇到困难. 这是因为您没有正确实现FB.getLoginStatus方法. 您要做的就是实现此方法

I don't think this solution will help you now, but i am posting it so that others will not have such a hard time. This is because you are not implementing the FB.getLoginStatus method properly. all you have to do is implement this method

 FB.getLoginStatus(function(response) {
    statusChangeCallback(response);
  });

初始化调用后的

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

after the init call. This method will call statusChangeCallback method and do the authentication.

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

for more reference you can refer this page of the official documentation https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.0

这篇关于FB JS-SDK无法检测到直接从FB注销的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆