您可以在不触发弹出窗口拦截器调用FB.login其他FB方法(如FB.getLoginStatus)回调里面呢? [英] Can you call FB.login inside a callback from other FB methods (like FB.getLoginStatus) without triggering popup blockers?

查看:2649
本文介绍了您可以在不触发弹出窗口拦截器调用FB.login其他FB方法(如FB.getLoginStatus)回调里面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立与FB的JavaScript SDK中的pretty基本验证逻辑流程,检查用户的登录状态,并执行操作(并提示用户之前的权限与权限登录,如果他们都没有)。


  1. 的用户类型的消息到我的网站上的textarea张贴到自己的Facebook的饲料,并点击了发布到Facebook按钮,在我的网站。

  2. 在回应一下,我检查用户的登录状态与 FB.getLoginStatus

  3. 在回调 FB.getLoginStatus ,如果用户没有登录,提示他们登录( FB.login )。

  4. 在回调 FB.login 然后我需要确保他们有合适的权限,所以我做出 FB.api通话('/ ME /权限') - 如果他们不这样做,我再次提示他们登录( FB.login

我遇到的问题是,任何时候我尝试调用 FB.login 回调到其他FB里面的方法,浏览器似乎失去轨道执行(点击),因此产地将阻止弹出。我不知道我是否错过了一些方法来提示用户检查自己的状态没有浏览器误以为它不是一个用户启动后弹出登录?

我目前回落到只调用 FB.login()先不管。这种方法的不想要的副作用,但是,如果用户已经有权限登录,我还在打电话 FB.login 中,auth弹出将打开,立即关闭,然后再继续,这看起来相当马车尽管是功能性。

这似乎是检查用户的登录状态和权限做的事情将是一个共同的流量,所以我觉得我失去了一些东西。下面是一些例子code。

 < D​​IV的onclick =onClickPostBtn()>发布到Facebook和LT; / DIV><脚本>
//回调点击后按钮。
功能onClickPostBtn(){    //检查是否登录,提示这样做,如果没有。
    FB.getLoginStatus(功能(响应){
        如果(response.status ==='连接'){
            checkPermissions(response.authResponse.accessToken);
        }其他{
            FB.login(函数(){},{范围:publish_stream'})
        }
    });
}//登录,检查权限。
功能checkPermissions(的accessToken){
    FB.api('/ ME /权限,
        {'ACCESS_TOKEN':}的accessToken,
        功能(响应){            //登录和授权的这个站点。
            如果(response.data&安培;&安培; response.data.length){                //解析响应对象,以检查许可这里...                如果(hasPermission){
                    //记录与权限,执行某些操作。
                }其他{
                    //登录时没有适当的权限,具有权限要求登录。
                    FB.login(函数(){},{范围:publish_stream'})
                }            //登录到FB,但无权对这个站点。
            }其他{
                FB.login(函数(){},{范围:publish_stream'})
            }
        }
    );
}
< / SCRIPT>


解决方案

  

我遇到的问题是,任何时候我尝试调用FB.login回调到其他FB里面的方法,浏览器似乎失去跟踪执行(点击)的起源,从而将阻止弹出窗口。


他实际上不是的失去的轨道的 - 这是一个完全不同的执行上下文的,因为FB方法工作的异步的。点击发生在被的的时间了,完成了一个执行上下文的被执行回调函数的时间。

您可能会提前检查权限,在页面加载。这是不太可能的东西对用户的特定权限的变化,而他对你的网站,我想 - 至少一个正常的用户,谁不试图乱用您的应用程序,看看它会做出反应。 (虽然有可能是使用情况下,这不能肯定地说。)

如果这导致了信息,一个必要的权限缺失 - 然后调用FB.login按钮点击询问此权限。在FB.login回调,您可以再次检查权限,如果你想以确保 - 如果仍然没有给予许可的然后的,那么我建议用户报警这一事实,也许告诉他再次为什么它是必要的 - 而且比他不得不点击重新开始整个操作的按钮

如果权限是正常的,那么你可以让你的API调用,实际上使一个职位。并要真的肯定,并在安全的地方......在API调用的回调可以再次检查成功或错误,如果有一个错误看看它缺少权限是因为 - 如果是这样,回到原点,告诉用户权限是必要的,让他重新开始整个事情,通过单击按钮...

I’m trying to set up a pretty basic authentication logic flow with the FB JavaScript SDK to check a user’s logged-in status and permissions before performing an action (and prompting the user to log in with permissions if they are not).

  1. The user types a message into a textarea on my site to post to their Facebook feed, and clicks a ‘post to Facebook’ button on my site.
  2. In response to the click, I check the user’s logged-in status with FB.getLoginStatus.
  3. In the callback to FB.getLoginStatus, if the user is not logged in, prompt them to log in (FB.login).
  4. In the callback to FB.login I then need to make sure they have the right permissions, so I make a call to FB.api('/me/permissions') — if they don’t, I again prompt them to log in (FB.login).

The problem I’m running into is that any time I try to call FB.login inside a callback to other FB methods, the browser seems to lose track of the origin of execution (the click) and thus will block the popup. I’m wondering whether I’m missing some way to prompt the user to login after checking their status without the browser mistakenly thinking that it’s not a user-initiated popup?

I’ve currently fallen back to just calling FB.login() first regardless. The undesired side effect of this approach, however, is that if the user is already logged in with permissions and I’m still calling FB.login, the auth popup will open and close immediately before continuing, which looks rather buggy despite being functional.

It seems like checking a user’s login status and permissions before doing something would be a common flow so I feel like I’m missing something. Here’s some example code.

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

解决方案

The problem I'm running into is that anytime I try to call FB.login inside a callback to other FB methods, the browser seems to lose track of the origin of execution (the click) and thus will block the popup.

He is not actually "losing track" – it’s a whole different execution context, because the FB methods work asynchronously. The click happened in an execution context that is long gone and finished by the time the callback function is executed.

You could check permissions earlier, on page load. It is not very likely that something about the user’s given permissions changes while he’s on your site, I guess – at least for a "normal" user, who does not try to mess with your app to see how it’ll react. (Although there might be uses cases where this can’t be said with certainty.)

If this results in the info that a necessary permission is missing – then call FB.login on the button click asking for this permission. In the FB.login callback, you can check the permissions again, if you want to be sure – if the permission is still not given then, then I’d suggest alerting the user to that fact, maybe telling him again why it’s necessary – and than he’ll have to click the button that starts the whole action again.

If permissions are alright, then you can make your API call to actually make a post. And to be really really sure and on the safe side … in that API call’s callback you can check for success or errors again, and if there’s an error find out if it was due to missing permissions – if so, back to square one, telling the user permission is necessary and have him start the whole thing over again, by clicking the button …

这篇关于您可以在不触发弹出窗口拦截器调用FB.login其他FB方法(如FB.getLoginStatus)回调里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆