15

我正在开发一个画布应用程序,我想使用 JS SDK 完成以下任务:

  • 检查用户是否授予了特定的扩展权限

    • 如果是这样,请调用一些函数 startServerProcess()

    • 如果没有,请显示身份验证对话框以获取权限,并在用户未提供足够访问权限时提醒用户。

我想在客户端完全处理这个问题,除非用户提供正确的权限,否则永远不会调用 startServerProcess(),因为它执行的服务器脚本依赖于这些权限。

搜索后我找到了这个解决方案,但它只使用我不想每次都调用的 FB.login() ,因为如果用户已经通过身份验证,则会打开一个烦人的身份验证对话框,然后立即自动关闭。

我最初的解决方案是调用 FB.getLoginStatus(),然后在可能的情况下对 /me/permissions 进行图形 api 调用,并且仅在我确实必须显示身份验证对话框时才调用 FB.login()。

这是我现在拥有的代码:

$('#my_ui_button').click(function() {
    require_perms(function(is_authenticated) {
        if(!is_authenticated) {
            startServerProcess();
        } else {
            alert('Sorry, we cannot start the server process until you grant permission');
        }
    });
});


function require_perms(callback) {
    // check if the user is logged in + connected to the app
    FB.getLoginStatus(function(response) {

        // if the user is logged in, continue to check permissions
        if(response.authResponse) {
            FB.api('/me/permissions', function(perms_response) {

                // if photo access already exists, we're good to go
                if(perms_response['data'][0]['user_photos'] && perms_response['data'][0]['friends_photos']) {
                    console.log('permissions are already granted.');
                    callback(true);

                    // photo access does not exist, so show an auth dialog
                } else {

                    // get photo permissions
                    console.log('requesting permission...');
                    FB.login(function(response) {
                        if(response.authResponse) {
                            console.log('permission granted');
                            callback(true);
                        } else {
                            console.log('permission request failed');
                            callback(false);
                        }
                    }, {
                        scope : 'user_photos,friends_photos'
                    });
                }
            });
            // user is not connected to the app, so show an auth dialog
        } else {

            // get photo permissions
            console.log('requesting permission...');
            FB.login(function(response) {
                if(response.authResponse) {
                    console.log('permission granted');
                    callback(true);
                } else {
                    console.log('permission request failed');
                    callback(false);
                }
            }, {
                scope : 'user_photos,friends_photos'
            });
        }
    });
}

这似乎是解决问题的一种混乱方式吗?我特别不喜欢我重复 FB.login() 代码的事实,但我遇到了一些问题,考虑到图形 api 调用的异步性质(或者我只是需要更多的睡眠)。

任何意见/建议将不胜感激。谢谢。

4

4 回答 4

1

I think this is significantly over-complicating what could just be achieved with FB.login(). FB.login() will only cause the pop up if the user is not logged in (so they will need it anyway) or if the session cookie isn't available. You could just call FB.getLoginStatus() after initialising the page which will make sure the cookie is added immediately if available.

The pop up is pretty much unavoidable, and is standard behaviour on most apps. I don't think your solution would gain much over simply:

FB.init({...});
FB.getLoginStatus();

then

FB.login({},{scope:...});
于 2011-09-13T10:37:50.650 回答
0

使用 oAuth 对话框。根据文档,它提示用户授予您的应用程序的权限。将用户的浏览器定向到 http://www.facebook.com/dialog/oauth/?scope=email,user_birthday&client_id=123050457758183&redirect_uri=http://www.example.com/response&response_type=token

检查此链接以获取完整文档:https ://developers.facebook.com/docs/reference/dialogs/oauth/

于 2011-09-13T10:48:29.013 回答
0

前两天我有完全相同的问题,但最后我解决了。在开发阶段,我只使用我自己的 facebook 帐户测试了应用程序,并且我没有看到 Facebook 登录屏幕有任何错误,但是当我开始使用其他 facebook 帐户(没有测试人员或开发人员用户)测试应用程序时我的应用程序),我遇到了这个问题。在我将这些用户作为测试人员(从应用程序->设置->开发人员角色)添加到我的应用程序后,它已修复。我希望这行得通。

于 2013-04-05T09:45:00.810 回答
0

我认为您的方法没有问题。不过,还应考虑的一个原因是浏览器兑现或服务器更新问题。确保您正在测试最新版本的代码。在新浏览器中测试应用程序通常是一个好主意。

于 2013-02-14T11:23:57.200 回答