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/ )
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);
}
});
});
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.
Some solution ? :)
From the documentation of Drive API, The error 400: Bad Request
can mean that a required field or parameter has not been provided, the value supplied is invalid, or the combination of provided fields is invalid.
This error can be thrown when trying to add a duplicate parent to a Drive item. It can also be thrown when trying to add a parent that would create a cycle in the directory graph.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
So in your case, the value that you provide in a field or parameter is invalid.
Check this SO question and some documentation to know more information about your issue.
Maybe it helps you, I`m using V4 of Google Drive API
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);
});
});
You need to use different mime types:
{
requestBody: {
...
mimeType: 'application/vnd.google-apps.document',
},
media: {
...
mimeType: 'text/html', // or text/plain
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.