简体   繁体   English

检测用户在使用gapi.auth.authorize时是否关闭弹出窗口

[英]Detect if user closes popup when using gapi.auth.authorize

Using the gapi.auth.authorize function, the user can close the popup without clicking any option (no accept or deny button). 使用gapi.auth.authorize函数,用户可以关闭弹出窗口而不单击任何选项(不接受或拒绝按钮)。 When this case happens, my callback function doesn't fire, so that I can't handle this case. 当这种情况发生时,我的回调函数不会触发,所以我无法处理这种情况。 What's the way to resolve this scenario? 解决这种情况的方法是什么?

Thanks. 谢谢。

They don't appear to mention it in any documentation, but gapi.auth.authorize() returns the popup Window . 他们似乎没有在任何文档中提及它,但gapi.auth.authorize()返回弹出Window So you can save the returned Window and set an interval or timeout to check Window.closed . 因此,您可以保存返回的Window并设置间隔或超时以检查Window.closed

So you the auth function from Google returns promise , not a window. 所以你从谷歌的auth功能返回承诺 ,而不是窗口。 But then you can wrap original window into the function, which will set interval, to check if opened window closed already. 但是你可以将原始窗口包装到函数中,该函数将设置间隔,以检查打开的窗口是否已经关闭。

// variable to store our deferred object
var authDefer = null;

function auth() {

    // right before the auth call, wrap window.open
    wrap();
    // Call auth
    authDefer = window.gapi.auth.authorize({
        client_id: ...,
        scope: ...,
        immediate: ...
    }).then(
        // onSuccess,
        // onReject,
        // onNotify
    );
}

function wrap() {
    (function(wrapped) {
        window.open = function() {
            // re-assign the original window.open after one usage
            window.open = wrapped;
            var win = wrapped.apply(this, arguments);
            var i = setInterval(function() {
                if (win.closed) {
                    clearInterval(i);
                    if (authDefer) {
                        authDefer.cancel();
                    }
                }
            }, 100);
            return win;
        };
    })(window.open);
}

Taken from one of the thread on Google forums. 摘自谷歌论坛上的一个主题。 Really works. 真的有用。

External link to Source Source的外部链接

This question has been around for a while, but when I looked into the issue (I want to show a spinner while the google authentication window is open, and hide it if the user decides not to authenticate), and found that gapi is throwing an error popup_closed_by_user . 这个问题已经存在了一段时间,但是当我调查这个问题时(我希望在谷歌身份验证窗口打开时显示一个微调器,如果用户决定不进行身份验证则隐藏它),并发现gapi正在抛出一个问题错误popup_closed_by_user There is a two-second delay (which is kind of long, Facebook's is instant) before it is thrown, but it does work. 在投掷之前有两秒钟的延迟(有点长,Facebook是即时的),但确实有效。 Hooray, Google! 万岁,谷歌!

Some sample code (angular 1.x), prompting is the attribute to show the spinner: 一些示例代码(angular 1.x), prompting是显示微调器的属性:

_google_obj.prompting = true;
gapi.auth2.getAuthInstance().signIn().then(function(googleResponse){
    var token = googleResponse.getAuthResponse().id_token;
    SVC_exec_.post('/q/goog', 1000, { token: token }, 'signing you in through Google', function (response) {
        if (response.code === 'ok') {
            // update the UI
        }
        _google_obj.prompting = false;
    });
}, 
function(error){
    $timeout(function () {
        console.log('user probably closed the google popup window: '+error);
        _google_obj.prompting = false;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用gapi.auth.authorize时访问被拒绝错误 - Access Denied Error when using gapi.auth.authorize gapi.auth.authorize:TypeError:cv(...)为null - gapi.auth.authorize: TypeError: cv(…) is null 带立即数的gapi.auth.authorize:true不起作用 - gapi.auth.authorize with immediate: true is not working gapi.auth.authorize:TypeError:_.Uu不是函数 - gapi.auth.authorize: TypeError: _.Uu is not a function 为什么带有“ immediate:false”的gapi.auth.authorize不关闭弹出窗口并触发回调? - Why gapi.auth.authorize with “immediate: false” doesn't close popup and fire callback? 在node-webkit应用程序上通过Google登录时,不会调用gapi.auth.authorize回调 - gapi.auth.authorize callback doesn't get called when doing sign-in via Google on node-webkit app 如何将login_hint传递给gapi.auth.authorize? - How do I pass login_hint to gapi.auth.authorize? Google OAuth gapi.auth.authorize X-Frame-Options:SAMEORIGIN - Google OAuth gapi.auth.authorize X-Frame-Options: SAMEORIGIN 对于 javascript 调用 api “gapi.auth.authorize” 的 google 分析没有得到执行。 - For javascript the call to api “gapi.auth.authorize” for google analytics not getting executed. 是否可以使用 javascript 打开弹出窗口,然后检测用户何时关闭它? - is it possible to open a popup with javascript and then detect when the user closes it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM