使用 javascript(无 html 表单)以编程方式将文件上传到 Google Drive

Nic*_*iks 2 javascript node.js google-drive-api

编辑:我是一个假人。文档在这里清楚地说明了这一点:https : //developers.google.com/drive/v3/web/manage-uploads

上下文的项目概述:我正在使用 node.js 编写部署在 Heroku 上的 twitter 机器人。我希望这个机器人能够从 Google Drive 下载 .txt 文件(或 .ini 或其他文件),使用该文件撰写推文,编辑文本文件的正文以反映生成的推文,然后覆盖 Google Drive 中的原始文件。它将始终下载、编辑和覆盖同一个文件,因此不需要文件选择对话框。

具体问题:我无法弄清楚如何在不使用 html 页面的情况下实际上传文本文件的正文。这一切都应该发生在没有用户交互的服务器端。我发现的大多数示例似乎都使用 html 页面来收集和发送文件信息,但我使用的是纯 javascript,因此这不适用于我的项目。我已经在 Google Drive API 文档中搜索了示例,但到目前为止还没有运气。

到目前为止的代码:

中间的评论块是我遇到麻烦的地方。

authClientUp.authorize(function(err, tokens) {
    if (err) {
        console.log(err);
        return;                                                     
    } else {
        console.log('It authorized successfully!');
        drive.files.insert({           // ---- For the purposes of getting
            auth: authClientUp,        // ---- started, I'm just creating
            "title": "file1.txt",      // ---- a new file instead of overwriting
            "mimeType": "text/plain",  // ---- an existing one
            "description": "Just a test file"
        }, function(err,result){
            if (err) {
                console.log(err);
            } else {
                console.log(result);
                drive.files.put({      //----- files.put() is not correct, but
                                       //----- I'm unsure what the correct function is
                    /*
                    I know I need the following info here but I'm 
                    not sure how to pass it in correctly:
                    - the id of the file to overwrite (taken from the JSON file 'result'
                    - the body of the file to upload
                    - authentication? I'm not sure if I need that agian
                    */

                }, function(err, result){
                    if (err) {
                        console.log(err);
                    } else {
                        console.log(result);
                    }
                });
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

我发现的所有示例都将这一步格式化为:

PUT /upload/drive/v2/files/{id}?uploadType=media HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer <OAuth 2.0 access token here>
Content-Type: mime/type

<file content here>
Run Code Online (Sandbox Code Playgroud)

但是在 html 文件的上下文之外,我认为这行不通?我需要帮助来为我的项目正确格式化。

笔记:

  • 将此文件写入 Google Drive 的目的是,我可以将更改提交到我的 Heroku 存储库,而不会覆盖和丢失 .txt 文件的内容
  • 我不是一个有经验的程序员,而且我是 javascript 新手。

我在 Stack Overflow 上搜索了许多其他问题,但它们似乎都取决于以下一项或多项内容:

  • 一个 html 表单
  • python,不是javascript
  • Google Drive Android SDK,我没有使用(虽然如果涉及到它,我想我可以)

fab*_*ous 5

检查此 Google Drive REST API 文档:

https://developers.google.com/drive/v3/web/manage-uploads

那里的例子,字面意思是:

var fileMetadata = {
  'name': 'photo.jpg'
};
var media = {
  mimeType: 'image/jpeg',
  body: fs.createReadStream('files/photo.jpg')
};
drive.files.create({
   resource: fileMetadata,
   media: media,
   fields: 'id'
}, function(err, file) {
  if(err) {
    // Handle error
    console.log(err);
  } else {
    console.log('File Id: ', file.id);
  }
});
Run Code Online (Sandbox Code Playgroud)