连接到 Google Drive API 时遇到问题

问题描述

我正在尝试连接到 Google Drive API,但我一直收到返回“未捕获的类型错误:无法在 initClient 读取未定义的属性 'init'。”

我不知道该怎么做才能解决这个问题。

这是我运行的代码

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

我按照建议进行了更改,但仍然无法正常工作。我不确定我做错了什么!这是更新后的代码

`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); },2));
            });
            
            return gapi;
        }

还有其他建议吗?谢谢!

解决方法

gapi.load 接受一个回调函数,但你传递给它的是 initClient() 的输出。

其次,您需要加载 OAuth2 库 (auth2)。

See reference docs

改变这个:

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

为此:

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

附注。如果您将代码放在问题的文本中,而不是屏幕截图中,那么其他人剪切粘贴答案会容易得多。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...