2017-08-24 125 views
0

我正在開發通過JavaScript使用Google Drive API。我嘗試了示例代碼,發現它工作正常。但是,相同的代碼不適用於Chrome擴展。我猜這是客戶端ID值的問題,但我不認爲這很奇怪,爲什麼它不適用於Chrome擴展?gapi.client.init(Google Drive API)爲什麼不起作用?

// 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

控制檯日誌不顯示 「客戶確認」

我已經註冊了擴展的地址是https://chrome.google.com/webstore/detail/study/minmfnjifbdpcphgildjgfpnbiejajml

+0

控制檯顯示什麼_does_?您是否正在尋找正確的控制檯(由您的案例中的Inspect Popup調用的控制檯)? – Xan

+0

控制檯日誌顯示「加載正常」和「客戶端」。我希望'客戶好'。奇怪的是,'gapi.client.init'方法沒有記錄日誌。 – megu

+0

請將[問題]置於主題上:包括[重複問題*的[mcve]]。對於Chrome擴展程序或Firefox Web擴展程序,幾乎總是需要包含* manifest.json *和一些背景,內容和/或彈出腳本/ HTML以及常用的網頁HTML /腳本。尋求調試幫助的問題(「爲什麼我的代碼不按我想要的方式工作?」)必須包括:(1)期望的行爲,(2)特定問題或錯誤,以及(3)重現它所需的最短代碼*在問題本身*。請參閱:[我可以在這裏詢問什麼主題?](/ help/on-topic)和[問]。 – Makyen

回答