使用Google Drive API和Node.js创建Google文档
Create a Google Document with Google Drive API and Node.js
我使用Google Drive API V3通过Node.js和Google-API-nodejs-client v12.0.0管理我的Google Drive(http://google.github.io/google-api-nodejs-client/)
当我尝试创建一个简单的文本/纯文本文档时,一切都很好。但当我试图创建一个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的错误。
一些解决方案?:)
从驱动器API的文档中,error 400: Bad Request
可能意味着未提供所需的字段或参数、提供的值无效或提供的字段组合无效。
当尝试将重复的父项添加到驱动器项目时,可能会引发此错误。当试图添加将在目录图中创建循环的父级时,也可以抛出它。
{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
因此,在您的情况下,您在字段或参数中提供的值是无效的。
查看此SO问题和一些文档以了解有关您的问题的更多信息。
谷歌驱动器:如何正确设置文件类型?
https://developers.google.com/drive/v3/web/mime-types
使用NODE.JS 创建文件
也许对你有帮助,我正在使用Google Drive API 的V4
var fileMetadata = {
'name': 'Project plan',
'mimeType': 'application/vnd.google-apps.document'
};
drive.files.create({
resource: fileMetadata,
fields: '*',
auth: jwtClient
}, function (err, file) {
if (err) {
return global.triggerError(req, res, err);
}
drive.permissions.create({
resource: {
'type': 'anyone',
'role': 'writer'
},
fileId: file.id,
fields: 'id',
auth: jwtClient
}, function (err, permission) {
if (err) {
return global.triggerError(req, res, err);
}
callback(file);
});
});
您需要使用不同的mime类型:
{
requestBody: {
...
mimeType: 'application/vnd.google-apps.document',
},
media: {
...
mimeType: 'text/html', // or text/plain
}
}
相关文章:
- 什么CSS/JS技巧驱动镜像反转http://com.google愚人节页面
- 通过内部数组将JSON数据导入D3.js+Google Map
- google.setOnLoadCallback()在单独的JS文件中不起作用
- 无法使用API-node.js从Google Drive下载文件
- "现代的“;在Google Maps JS API v3上使用字母标记
- Google PageSpeed Insights说“删除渲染阻塞js文件”
- 在 iFrame 中注入 JS 文件,通过 Google chrome 扩展名中的 ContentScript 进行
- 使用 node.js 从 Google 云端硬盘读取二进制文件
- Google Maps JS API v3 - Add Map Types
- 在 Google Compute Engine Debian 服务器上运行 Node.js
- 将JS变量作为文本返回以发送到Google Analytics
- Slick.js不适用于Google Maps API中的infoBox.js
- 如何使用地点ID从Google Places JS API检索地点详细信息
- 授权客户端JS API调用Google'的具有现有访问令牌的gap库
- 如何在Java中使用Google Caja HTML/CSS消毒JS库
- Google JS API: gapi.auth.signIn 回调函数问题
- Google+JS API自动触发交互式帖子按钮
- 加载Google JS api时阻止了跨来源请求
- 如何在google JS测试中模拟jQuery选择器
- Google JS api与JQuery或Mootools有冲突吗?