简体   繁体   English

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

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

My application uses Facebook authentication: 我的应用使用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'});

And when using it, people can post to their wall: 使用它时,人们可以发布到他们的墙上:

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

This works fine, but there is one little hickup. 这样可以正常工作,但有一个小小的提升。 If people: 如果人:

  1. Log in on the app. 登录该应用程序。
  2. Log out of Facebook. 退出Facebook。
  3. Attempt to make a wall post from the app. 尝试从应用程序发布一个墙贴。

The opening of the wall post dialog fails. 墙贴对话框的打开失败。 The console says " Refused to display document because display forbidden by X-Frame-Options. ". 控制台显示拒绝显示文档,因为X-Frame-Options禁止显示 ”。

Can I instead get Facebook to show a login prompt to the user. 我可以让Facebook向用户显示登录提示。 Or can I detect the error and tell the user that he's no longer logged in on Facebook? 或者我可以检测到错误并告诉用户他已不再登录Facebook吗?

Just recall getLoginStatus BUT forcing a roundtrip to Facebook. 回想一下getLoginStatus但强行往返Facebook。 Look following code: 看下面的代码:

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

Look the last parameter set to true to force the roundtrip. 查看最后一个设置为true的参数以强制进行往返。

From JS SDK documentation: 来自JS SDK文档:

To improve the performance of your application, not every call to check the status of the user will result in request to Facebook's servers. 为了提高应用程序的性能,并非每次检查用户状态的调用都会导致对Facebook服务器的请求。 Where possible, the response is cached. 在可能的情况下,缓存响应。 The first time in the current browser session that FB.getLoginStatus is called, or the JS SDK is init'd with status: true, the response object will be cached by the SDK. 在当前浏览器会话中第一次调用FB.getLoginStatus ,或者JS SDK初始化为status:true,响应对象将由SDK缓存。 Subsequent calls to FB.getLoginStatus will return data from this cached response. FB.getLoginStatus的后续调用将从此缓存的响应中返回数据。

This can cause problems where the user has logged into (or out of) Facebook since the last full session lookup, or if the user has removed your application in their account settings. 这可能会导致用户自上次完整会话查询以来登录(或退出)Facebook的问题,或者用户已在其帐户设置中删除了您的应用程序。

To get around this, you call FB.getLoginStatus with the second parameter set to true to force a roundtrip to Facebook - effectively refreshing the cache of the response object. 为了解决这个问题,你可以调用FB.getLoginStatus并将第二个参数设置为true来强制往返于Facebook - 有效地刷新响应对象的缓存。 ( http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/ ) http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

What you could try and use is the FB.getLoginStatus where if the user is connected this would allow them to complete the wall post. 您可以尝试使用的是FB.getLoginStatus,如果用户已连接,这将允许他们完成墙贴。 If they aren't connected then call the FB.login method before they can post on the wall. 如果它们没有连接,那么在他们可以在墙上发布之前调用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/ http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

There are also the events for login and logout that you can watch for and do something with those responses. 您还可以查看登录和注销事件,并对这些响应执行某些操作。

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/ http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM