我正在使用 gapi客户端在 chrome 扩展中访问 Google Drive。第一步是授权我的应用程序。我正在使用 gapi.auth.authorize 来启动授权。在我通过 gapi 启动的弹出窗口授权应用程序后,窗口永远不会关闭并卡住,如下图所示。但是在后台授权成功了,因为如果我手动关闭窗口,下次我就看不到卡住的弹出窗口了。有人可以指出我做错了什么吗?
在授权弹出窗口中点击“允许访问”后,
然后显示卡住的空白弹出窗口
我正在使用的代码
function handleClientLoad(){
gapi.client.setApiKey('My API key');
window.setTimeout(checkAuthAuto, 1);
}
var checkAuthAuto = function () {
console.log('checkAuthAuto');
gapi.auth.authorize({
client_id: 'My client id',
scope: 'https://www.googleapis.com/auth/drive.file',
immediate: true
}, handleAuthResult);
}
function handleAuthResult(authResult) {
console.log('handleAuthResult');
var authButton = document.getElementById('authorizeButton');
var filePicker = document.getElementById('filePicker');
var addButton = document.getElementById('addButton');
authButton.style.display = 'none';
filePicker.style.display = 'none';
addButton.style.display = 'none';
if (authResult && !authResult.error) {
addButton.style.display = 'block';
addButton.onclick = uploadFile;
console.log('handleAuthResult:noerror');
} else {
// No access token could be retrieved, show the button to start the authorization flow.
authButton.style.display = 'block';
console.log('handleAuthResult:error');
console.log(authResult);
authButton.onclick = function() {
console.log('authButton.onclick');
gapi.client.setApiKey('My api key');
gapi.auth.authorize({
client_id: 'My client id',
scope: 'https://www.googleapis.com/auth/drive.file',
immediate: false
}, handleAuthResult);
return false;
};
}
}
最佳答案
即使使用 chrome 扩展程序,您也可以使用 chrome.identity 并且弹出窗口会像我们预期的那样关闭。
使用名为 gapi.auth.authorize 的包装器包装 chrome.identity.getAuthToken。 引用:https://github.com/GoogleChrome/chrome-app-samples/blob/master/gapi-chrome-apps-lib/gapi-chrome-apps.js
问题是当前的稳定版 chrome 28 不允许您这样做,您最好进行版本检查或等待一段时间,直到稳定版获得此功能。 尽管如此,仍然不确定,即使我能够做到这一点,使用扩展来做这件事也是可以的......
关于javascript - 谷歌授权弹出窗口卡在 chrome 扩展中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15266305/