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

时间:2013-03-07 08:24:16

标签: javascript google-api google-drive-api google-api-js-client

我在Chrome扩展程序中使用gapi客户端来访问Google云端硬盘。第一步是授权我的应用程序。我正在使用gapi.auth.authorize来启动授权。在我通过gapi启动的弹出窗口授权应用程序后,窗口永远不会关闭并卡住,如下图所示。然而,授权在后台成功,因为如果我手动关闭窗口,下次我不会看到卡住的弹出窗口。有人可以指出我做错了什么吗?

点击'允许访问权限'在授权弹出窗口中,

enter image description here

然后会显示一个空白的弹出窗口

Popup gets stuck

我正在使用的代码

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;
          };
        }
}

1 个答案:

答案 0 :(得分: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不允许你这样做,你也可以进行版本检查或等待一段时间,直到稳定版获得此功能。 尽管如此仍然不确定,即使我能够做到这一点,使用扩展这样做也是可以的......