3

我的应用程序使用 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?

4

2 回答 2

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/

于 2013-04-26T02:38:03.317 回答
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/

于 2012-02-28T13:57:24.697 回答