javascript - 使用 Google Drive API 和 Node.js 创建 Google 文档

标签 javascript node.js google-api google-drive-api google-api-nodejs-client

我正在使用 Google Drive API V3 通过 Node.js 和 google-api-nodejs-client v12.0.0 ( http://google.github.io/google-api-nodejs-client/ ) 管理我的 Google 云端硬盘

当我尝试创建一个简单的文本/纯文本文档时,一切正常。但是,当我尝试创建 Google 文档时,Google Drive API 返回 400 错误,并显示消息“错误请求”。

/**
   * Create file on Google Drive
   * https://developers.google.com/drive/v3/reference/files/create
   */
  CreateFile: (googleapi, oauth2Client, fileName, fileContent, fileType) => {
    const DRIVE = googleapi.drive({ version: 'v3', auth: oauth2Client });

    return new Promise((resolve, reject) => {
      console.log('fileType:',fileType);
      DRIVE.files.create({
        resource: {
          name: fileName,
          mimeType: fileType
        },
        media: {
          mimeType: fileType,
          body: fileContent
        }
      }, (err, result) => {
        if( err ) {
          reject(err);
        }
        else {
          resolve(result);
        }
      });
    });

当变量“fileType”的值为“text/plain”时,一切正常。但是当我将“application/vnd.google-apps.document”放入该值时,我收到了 400 错误。

一些解决方案? :)

最佳答案

来自documentation在 Drive API 中,错误 400:错误请求 可能意味着未提供必填字段或参数、提供的值无效或提供的字段组合无效。

尝试向云端硬盘项目添加重复的父项时,可能会引发此错误。当尝试添加会在目录图中创建循环的父级时,也可能会抛出该错误。

{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}

因此,在您的情况下,您在字段或参数中提供的值无效。

检查此问题和一些文档以了解有关您的问题的更多信息。

关于javascript - 使用 Google Drive API 和 Node.js 创建 Google 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703265/

相关文章:

Javascript。交换两个变量。怎么运行的?

javascript - 如何使用带钩子(Hook)的 react 功能组件测试句柄函数调用

javascript - 当我单击 anchor 按钮时显示一个 div 并隐藏其他

node.js - 为 require() 配置多个路径

c++ - npm安装pomelo错误VC++ 2010?

google-api - Google Calendar API V3 insert_calendar 返回 503,但日历插入成功

javascript - 来自 python dataframe 的 Google 图表无法绘制

node.js - axios get请求mongodb返回空数据

api - Blogger Api V3 中的分页

javascript - 如何在 Google map 中添加缩放控件