1

I am using Node.js google-api client for creating file in google drive normally when i create file everything work fine , here is an example :

drive.files.create({
      resource: {
        name: 'Another File 5',
        mimeType: 'text/plain',
      },
      media: {
        mimeType: 'text/plain',
        body: 'It\'s Another Fucking File',
      }

    }, function(err,result){
        if(err) console.log(err) 
        else console.log(result)
    });

Now i want create a shared "shared": true its give me this error :

drive.files.create({
  resource: {
    name: 'Another File 5',
    mimeType: 'text/plain',
    "shared": true
  },
  media: {
    mimeType: 'text/plain',
    body: 'It\'s Another Fucking File',
  }

}, function(err,result){
    if(err) console.log(err) 
    else console.log(result)
});

Error :

{ [Error: The resource body includes fields which are not directly writable.]
  code: 403,
  errors: 
   [ { domain: 'global',
       reason: 'fieldNotWritable',
       message: 'The resource body includes fields which are not directly writable.' } ] }

I tried this in Google APIs Explorer and gave same error.

I am new to google Api and appreciate any help.

user1086010
  • 666
  • 1
  • 9
  • 25

1 Answers1

4

There's no shared attribute on the request body of create. This has been discussed

Its discussed int he Share Files section of the Drive API documentation, you'll need to call drive.permissions.create and set the appropriate permission of the account you'll share with.

I tested this on the API Explorer as well, and it works.

var fileId = '1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ';
drive.permissions.create({
    resource: {
        'type': 'user',
        'role': 'writer',
        'emailAddress': 'example@appsrocks.com'
    }, 
        fileId: fileId,
        fields: 'id',
    }, function(err, res) {
    if (err) {
        // Handle error
        console.log(err);
    } else {
        console.log('Permission ID: ', res.id)
        drive.permissions.create({
            resource: {
                'type': 'domain',
                'role': 'reader',
                'domain': 'appsrocks.com'
            },
            fileId: fileId,
            fields: 'id',
            }, function(err, res) {
                if (err) {
                // Handle error
                console.log(err);
            } else {
                console.log('Permission ID: ', res.id)
            }
        });
    }
});
adjuremods
  • 2,938
  • 2
  • 12
  • 17
  • i knew about `drive.permissions.create` and already using this , i just want know this is possible and apparently it's not. – user1086010 Sep 30 '16 at 12:22