为什么gapi.client.init(Google Drive API)不起作用?

时间:2017-08-24 10:20:47

标签: javascript google-chrome-extension google-drive-api

我正在通过JavaScript使用Google Drive API进行开发。我尝试了示例代码,发现它工作正常。但是,相同的代码不适用于chrome-extension。我想这是客户端ID值的问题,但我不认为这很奇怪,为什么它不适用于Chrome扩展程序?

  // Client ID and API key from the Developer Console
  var CLIENT_ID = '467319036759-udrp5jb3257p1kd11n5016rdid4qm8kd.apps.googleusercontent.com';

  // Array of API discovery doc URLs for APIs used by the quickstart
  var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];

  // Authorization scopes required by the API; multiple scopes can be
  // included, separated by spaces.
  var SCOPES = 'https://www.googleapis.com/auth/drive';

  var authorizeButton = document.getElementById('authorize-button');
  var signoutButton = document.getElementById('signout-button');

  /**
   *  On load, called to load the auth2 library and API client library.
   */
  function handleClientLoad() {
    gapi.load('client:auth2', initClient);
    console.log("load ok");
  }

  /**
   *  Initializes the API client library and sets up sign-in state
   *  listeners.
   */
  function initClient() {
    gapi.client.init({
      discoveryDocs: DISCOVERY_DOCS,
      clientId: CLIENT_ID,
      scope: SCOPES
    }).then(function () {
      // Listen for sign-in state changes.
      console.log("client ok");
      gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);

      // Handle the initial sign-in state.
      updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());

      authorizeButton.onclick = handleAuthClick;
      signoutButton.onclick = handleSignoutClick;
    }, function(error) {
      console.log(error);
    });

    console.log("client");
  }

enter image description here

控制台日志未显示“client ok”

我注册的扩展程序的地址是https://chrome.google.com/webstore/detail/study/minmfnjifbdpcphgildjgfpnbiejajml

1 个答案:

答案 0 :(得分:0)