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

问题描述:

我正在开发通过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