Google授权弹出窗口卡在Chrome扩展程序中

pls*_*spl 6 javascript google-api google-drive-api google-api-js-client

我在Chrome扩展程序中使用gapi客户端来访问Google云端硬盘.第一步是授权我的应用程序.我正在使用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;
          };
        }
}
Run Code Online (Sandbox Code Playgroud)

goo*_*yun 0

即使使用 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 不允许您执行此操作,您最好进行版本检查或等待一段时间,直到稳定版本获得此功能。但仍然不确定,即使我能够做到这一点,使用扩展来做这件事也没关系......