如何检测用户登录我的应用程序后从Facebook注销

How to detect user logging out of Facebook after logging into my app?

本文关键字:应用程序 我的 注销 Facebook 登录 用户 何检测 检测      更新时间:2023-09-26

我的应用程序使用Facebook身份验证:

FB.init({
    appId: config.fbAppId,
    status: true,
    cookie: true,
//  xfbml: true,
//  channelURL : 'http://WWW.MYDOMAIN.COM/channel.html', // TODO
    oauth  : true
});
// later...
FB.login(function(response)
{
    console.log(response);
    console.log("authId: " + response.authResponse.userID);
    gameSwf.setLoginFacebook(response.authResponse.accessToken);
}, {scope:'email,publish_actions,read_friendlists'});

当使用它时,人们可以在墙上张贴:

var obj = {
      method: 'feed',
      link: linkUrl,
      picture: pictureUrl,
      name: title,
      caption: "",
      description: message
    };
    function callback(response) {
      // console.log("Post on wall: " + response);
    }
    FB.ui(obj, callback);

这很好用,但有一个小问题。如果人员:

  1. 登录应用程序
  2. 注销Facebook
  3. 尝试从应用程序制作墙帖

墙柱对话框打开失败。控制台显示"拒绝显示文档,因为X-Frame-Options禁止显示。"。

我可以让Facebook向用户显示登录提示吗。或者我可以检测到错误并告诉用户他不再登录Facebook吗?

只需回忆getLoginStatus,但强制往返Facebook。查看以下代码:

FB.getLoginStatus(function(response) {
  // some code
}, true);

查看设置为true的最后一个参数以强制往返。

来自JS SDK文档:

为了提高应用程序的性能,不是每次调用检查用户的状态将导致向Facebook的服务器。在可能的情况下,会缓存响应。第一次在FB.getLoginStatus被调用的当前浏览器会话,或JSSDK初始化状态为true,响应对象将由SDK。后续对FB.getLoginStatus的调用将从此缓存的响应。

这可能会导致用户登录(或注销)时出现问题自上次完整会话查找以来的Facebook,或者如果用户在他们的帐户设置中删除了你的应用程序。

为了解决这个问题,您调用FB.getLoginStatus参数设置为true以强制往返Facebook-有效刷新响应对象的缓存。(http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/)

您可以尝试使用FB.getLoginStatus,如果用户已连接,这将允许他们完成墙柱。如果他们没有连接,那么在他们可以在墙上发帖之前,请调用FB.login方法。

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        // the user is logged in and has authenticated your
        // app, and response.authResponse supplies
        // the user's ID, a valid access token, a signed
        // request, and the time the access token 
        // and signed request each expire
        var uid = response.authResponse.userID;
        var accessToken = response.authResponse.accessToken;
    } else if (response.status === 'not_authorized') {
        // the user is logged in to Facebook, 
        // but has not authenticated your app
    } else {
        // the user isn't logged in to Facebook.
    }
});

http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

还有一些登录和注销事件,您可以监视这些响应并对其进行处理。

FB.Event.subscribe('auth.login', function(response) {
    // do something with response
});
FB.Event.subscribe('auth.logout', function(response) {
    // do something with response
});

http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/