简体   繁体   English

无法连接到 Google Drive API

[英]Having issues to connect to Google Drive API

I am trying to connect to Google Drive API but I keep getting the return "Uncaught TypeError: Cannot read property 'init' of undefined at initClient."我正在尝试连接到 Google Drive API,但我不断收到返回“未捕获的类型错误:无法在 initClient 读取未定义的属性 'init'。”

I am not sure what to do to fix this.我不知道该怎么做才能解决这个问题。

Here is the code I was running:这是我正在运行的代码:

       `enter code here`var CLIENT_ID = ' ';
        var API_KEY = ' ';          
        // 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.readonly';
  
        
  
        var FILES = {};
  
        function handleClientLoad() {
            setTimeout(function(){ gapi.load('client', initClient()); }, 3000);
        }
  
        function initClient() {
            gapi.client.init({
                apiKey: API_KEY,
                clientId: CLIENT_ID,
                discoveryDocs: DISCOVERY_DOCS,
                scope: SCOPES
            }).then(function () {
                // Listen for sign-in state changes.
                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(JSON.stringify(error, null, 2));
            });
            
            return gapi;
        }

I changed it as suggested but it's still not working.我按照建议更改了它,但它仍然无法正常工作。 I am not sure what I did wrong: Here is the updated code:我不确定我做错了什么:这是更新的代码:

`enter code here`var CLIENT_ID = ' ';
        var API_KEY = ' ';          
        // 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.readonly';
  
        
  
        var FILES = {};
  
        function handleClientLoad() {
            setTimeout(function(){ gapi.load('client:auth2', initClient); }, 3000);
        }
  
        function initClient() {
            gapi.client.init({
                apiKey: API_KEY,
                clientId: CLIENT_ID,
                discoveryDocs: DISCOVERY_DOCS,
                scope: SCOPES
            }).then(function () {
                // Listen for sign-in state changes.
                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(JSON.stringify(error, null, 2));
            });
            
            return gapi;
        }

Any other suggestions?还有其他建议吗? Thank you!谢谢!

gapi.load takes a callback function, but you're passing it the output of initClient(). gapi.load 接受一个回调 function,但您将它传递给 initClient() 的 output。

Secondly, you will need to load the OAuth2 library (auth2).其次,您需要加载 OAuth2 库 (auth2)。

See reference docs 请参阅参考文档

Change this:改变这个:

function handleClientLoad() {
  setTimeout(function() { gapi.load('client', initClient()); }, 3000);

To this:对此:

function handleClientLoad() {
  setTimeout(function() { gapi.load('client:auth2', initClient); }, 3000);

PS. PS。 If you put your code in the text of your question, rather than a screenshot, it's a lot easier for others to cut-paste an answer.如果您将代码放在问题的文本中,而不是屏幕截图中,其他人更容易剪切粘贴答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM