检测错误:“popup_blocked_by_browser”为谷歌auth2在JavaScript
问题描述:
很多谷歌搜索没有找到解决方案如何捕捉窗口弹出窗口拦截器的错误谷歌auth2检测错误:“popup_blocked_by_browser”为谷歌auth2在JavaScript
越来越控制台错误的错误后:“popup_blocked_by_browser”。 我想要做的就是告诉用户应该启用弹出来进行身份验证。
使用window.open()的样本不好,因为它们打开无用的窗口。当我看到很多人在寻找这个。
有什么建议吗?
答
终于!! signIn()方法使用JS Promise。所以代码可以使用的是:
gapi.auth2.getAuthInstance().signIn().then(function(){}, function(error){ if (error) alert('please allow popup for this app')})
希望这会有所帮助!
答
有同样的问题。似乎浏览器(或至少铬)将阻止任何“window.open”调用,它不是作为用户交互的一部分被调用。
请参阅here以获取更详细的说明。
__
我曾经有点击事件监听器里这下代码:
gapi.load('auth2', function() {
var auth2 = gapi.auth2.init({
client_id: '.apps.googleusercontent.com',
fetch_basic_profile: true,
scope: 'profile'
});
auth2.signIn()
.then(function() {
var profile = auth2.currentUser.get().getBasicProfile();
...
})
.catch(function(err) { ... });
});
注装载的异步方式“auth2”,这是谷歌实况怎么说。
我把它改为:
// way earlier, before click event can happen
// we call the gapi.load method.
gapi.load('auth2', function() {});
然后,内单击事件处理程序,我们可以这样做:
var auth2 = gapi.auth2.init({
client_id: '.apps.googleusercontent.com',
fetch_basic_profile: true,
scope: 'profile'
});
auth2.signIn()
.then(function() { ... })
.catch(function(err) { ... });
...所以浏览器不阻止谷歌登录弹出
祝谷歌文档只会说哪些方法是异步或同步。完全不明显,'gapi.auth2.init'是同步的......感谢您的答案! – Iest