javascript - fb.login 在后台运行对话框?

标签 javascript jquery facebook facebook-javascript-sdk

为了检查用户是否提供了我的应用程序所需的所有权限,我这样做:

/*Solo hacemos la peticion si el checkbox estaba desmarcado. Nota: esta funcion asume que solo hay un checkbox en el dom*/
if($('input:checkbox').prop('checked')){
    FB.login(function(response){
             console.log(response.status);
            if (response.status == 'connected') {
            hi   /* Entonces listo */
            }else{
                 /* Entonces cancelamos el checkbox */
                 $('input:checkbox').removeAttr('checked');
            }
    }, { scope: 'publish_stream' });
}

这样:

  • 如果用户已准备好:弹出窗口打开时间不到一秒

  • 如果用户还没有:显示权限对话框

问题是:在第一种情况下如何防止打开对话框(可能在后台运行)?

最佳答案

使用 FB.login 时无法停止弹出窗口。

使用服务器端身份验证或使用 FB.getLoginStatus 来防止在第一种情况下弹出。

FB.getLoginStatus allows you to determine if a user is logged in to Facebook and has authenticated your app

引用Facebook docs

上述引用链接的片段,

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

这就是您所需要的。

编辑:(OP评论后)

可能 Facebook 正在缓存旧结果。 来 self 上面提到的同一链接,

Roundtrips to Facebook's servers

To improve the performace of your application, not every call to check the status of the user will result in request to Facebook's servers. Where possible, the response is cached. The first time in the current browser session that FB.getLoginStatus is called, or the JD SDK is init'd with status: true, the response object will be cached by the SDK. Subsequent calls to FB.getLoginStatus will return data from this cached response.

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.

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(function(response) {
      // this will be called when the roundtrip to Facebook has completed
    }, true);

If you call FB.getLoginStatus on every page load, be careful not to set this parameter for each as it will significantly increase the number of requests to Facebook's servers, and thus decrease the performace of your application.

关于javascript - fb.login 在后台运行对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10686740/

相关文章:

Javascript 使用输入变量访问数组中的数组

javascript - jQuery 数据表处理 Null 子对象

javascript - jQuery 更改输入文本值不起作用

JQuery 使按下的按钮处于事件状态,而所有其他按钮处于非事件状态

android - 从 Facebook Graph-API 获取个人资料图片

javascript - 是否可以从 javascript 获取网络掩码?

javascript - 将多个 http.requests 中的数组组合成一个数组

javascript - Bootstrap 弹出窗口卡在全日历中

javascript - Facebook 视频上传错误 1363030

android - Facebook 安卓 : passing facebook object between activities