Skip to content Skip to sidebar Skip to footer

Create A Google Document With Google Drive API And Node.js

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

Solution 1:

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.


Solution 2:

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);
            });
        });

Solution 3:

You need to use different mime types:

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

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

Post a Comment for "Create A Google Document With Google Drive API And Node.js"