登录我的应用程序后如何检测用户登出Facebook?

时间:2012-02-28 13:34:59

标签: javascript facebook facebook-ui

我的应用程序使用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. 尝试从应用中发帖墙。
  4. 墙贴对话框的打开失败。控制台说“拒绝显示文档,因为X-Frame-Options禁止显示。”。

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

2 个答案:

答案 0 :(得分:4)

回想一下 getLoginStatus 但强行往返Facebook。请查看以下代码:

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

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

来自JS SDK文档:

  

要提高应用程序的性能,请不要每次调用   检查用户的状态将导致对Facebook的请求   服务器。在可能的情况下,缓存响应。第一次来了   调用 FB.getLoginStatus 的当前浏览器会话,或JS   SDK初始化为status:true,响应对象将被缓存   SDK。对 FB.getLoginStatus 的后续调用将从中返回数据   这个缓存的回复。

     

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

     

要解决此问题,请使用第二个调用 FB.getLoginStatus   参数设置为true以强制往返于Facebook   刷新响应对象的缓存。   (http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

答案 1 :(得分:2)

您可以尝试使用的是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/

相关问题