使用Google Drive API和Node.js创建Google文档 [英] Create a Google Document with Google Drive API and Node.js

查看:86
本文介绍了使用Google Drive API和Node.js创建Google文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Drive API V3通过Node.js和google-api-nodejs-client v12.0.0(

I'm using Google Drive API V3 to manage my Google Drive with Node.js and google-api-nodejs-client v12.0.0 (http://google.github.io/google-api-nodejs-client/)

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

When I try to create a simple text/plain document, all work fine. But when I try to create a Google Document, Google Drive API return a 400 error with message "Bad Request".

/**
   * 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错误.

When the value of variable "fileType" is "text/plain", all is ok. But when I put "application/vnd.google-apps.document" to the value, I got a 400 error.

一些解决方案?:)

推荐答案

您需要使用不同的mime类型:

You need to use different mime types:

{
  requestBody: {
    ...
    mimeType: 'application/vnd.google-apps.document',
  },

  media: {
    ... 
    mimeType: 'text/html', // or text/plain
  }
}

这篇关于使用Google Drive API和Node.js创建Google文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆