0

我正在尝试使用 FB JavaScript SDK 设置一个非常基本的身份验证逻辑流程,以在执行操作之前检查用户的登录状态和权限(如果没有,则提示用户使用权限登录)。

  1. 用户在我的站点上的文本区域中键入一条消息以发布到他们的 Facebook 提要,然后单击我站点上的“发布到 Facebook”按钮。
  2. 为了响应点击,我使用 . 检查用户的登录状态FB.getLoginStatus
  3. 在对 的回调中FB.getLoginStatus,如果用户未登录,则提示他们登录(FB.login)。
  4. 在回调中,FB.login我需要确保他们拥有正确的权限,所以我打电话给FB.api('/me/permissions') - 如果他们没有,我再次提示他们登录 ( FB.login)。

我遇到的问题是,每当我尝试FB.login在回调中调用其他 FB 方法时,浏览器似乎都会忘记执行的来源(点击),因此会阻止弹出窗口。我想知道我是否缺少某种方式在检查用户状态后提示用户登录,而浏览器不会错误地认为它不是用户启动的弹出窗口?

我目前已经退回到只是FB.login()先打电话。但是,这种方法的不良副作用是,如果用户已经使用权限登录并且我仍在调用FB.login,则身份验证弹出窗口将在继续之前立即打开和关闭,尽管功能正常,但看起来相当有问题。

在做某事之前检查用户的登录状态和权限似乎是一种常见的流程,所以我觉得我错过了一些东西。这是一些示例代码。

<div onclick="onClickPostBtn()">Post to Facebook</div>

<script>
// Callback to click on Post button.
function onClickPostBtn() {

    // Check if logged in, prompt to do so if not.
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            checkPermissions(response.authResponse.accessToken);
        } else {
            FB.login(function(){}, {scope: 'publish_stream'})
        }
    });
}

// Logged in, check permissions.
function checkPermissions(accessToken) {
    FB.api('/me/permissions',
        {'access_token': accessToken},
        function(response){

            // Logged in and authorized for this site.
            if (response.data && response.data.length) {

                // Parse response object to check for permission here...

                if (hasPermission) {
                    // Logged in with permission, perform some action.
                } else {
                    // Logged in without proper permission, request login with permissions.
                    FB.login(function(){}, {scope: 'publish_stream'})
                }

            // Logged in to FB but not authorized for this site.
            } else {
                FB.login(function(){}, {scope: 'publish_stream'})
            }
        }
    );
}
</script>
4

2 回答 2

2
于 2012-08-02T15:13:00.547 回答
0

我有另一种解决方案,可以更优雅地检查用户是否有

  • A) 登录 FB
  • B) 正确的权限

简而言之,对于 A),您需要“提醒”用户他没有登录,而不是“回调”Fb.login。然后您的页面应该刷新,并在刷新期间处理登录过程。

但是,对于 B) 权限,您使用 fb.ui 方法(使用 display: iframe)请求权限(通过回调),而不是使用 fb.login 来请求权限。使用 display:iframe 应该可以避免弹出窗口拦截器的麻烦。

请看下面修改后的代码:

<div onclick="onClickPostBtn()">Post to Facebook</div>

<script>
// Callback to click on Post button.
function onClickPostBtn() {

    // Check if logged in, prompt to do so if not.
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            checkPermissions(response.authResponse.accessToken);
        } else {
            //Don't call FB.login because this results in popup blocker.
            //FB.login(function(){}, {scope: 'publish_stream'})
            //Instead, just alert user that "YOu are not connected to FB", and then refresh the page (which should theoreticalyl handle the logging aspect)
        }
    });
}

// Logged in, check permissions.
function checkPermissions(accessToken) {
    FB.api('/me/permissions',
        {'access_token': accessToken},
        function(response){

            // Logged in and authorized for this site.
            if (response.data && response.data.length) {

                // Parse response object to check for permission here...

                if (hasPermission) {
                    // Logged in with permission, perform some action.
                } else {
                    //Don't use FB.login to request permissions. Instead use a FB.ui ({method:'permissions.request'} with "display" set to "iframe"
                    //FB.login(function(){}, {scope: 'publish_stream'})

                    FB.ui(
                    {
                         method: 'permissions.request',
                         'perms': 'publish_stream,WHATEVER_PERMISSIONS',
                         'display': 'iframe' //THIS IS IMPORTANT TO PREVENT POPUP BLOCKER
                    },
                    function(response) {
                        //Do action if permissions add success other wise prompt error adding permissions.
                    }
                }

            // Logged in to FB but not authorized for this site.
            } else {

                //Don't call FB.login because this results in popup blocker.
                //FB.login(function(){}, {scope: 'publish_stream'})
                //Instead, just alert user that "YOu are not connected to FB", and then refresh the page (which should theoreticalyl handle the logging aspect)
            }
        }
    );
}
</script>
于 2012-10-12T03:21:39.210 回答