2017-08-10 127 views
1

很多谷歌搜索沒有找到解決方案如何捕捉窗口彈出窗口攔截器的錯誤谷歌auth2檢測錯誤:「popup_blocked_by_browser」爲谷歌auth2在JavaScript

越來越控制檯錯誤的錯誤後:「popup_blocked_by_browser」。 我想要做的就是告訴用戶應該啓用彈出來進行身份驗證。

使用window.open()的樣本不好,因爲它們打開無用的窗口。當我看到很多人在尋找這個。

有什麼建議嗎?

回答

0

終於!! signIn()方法使用JS Promise。所以代碼可以使用的是:

gapi.auth2.getAuthInstance().signIn().then(function(){}, function(error){ if (error) alert('please allow popup for this app')}) 

希望這會有所幫助!

1

有同樣的問題。似乎瀏覽器(或至少鉻)將阻止任何「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) { ... }); 

...所以瀏覽器不阻止谷歌登錄彈出

+0

祝谷歌文檔只會說哪些方法是異步或同步。完全不明顯,'gapi.auth2.init'是同步的......感謝您的答案! – Iest