Search code examples
javascriptpermissionsgoogle-drive-api

transfer file ownership in Google Drive API


I am trying to transfer file ownership to a particular email address.

I send this request first:

var body = {
    'emailAddress': value,
    'type': type,
    'role': "writer"
  };
  var request = gapi.client.drive.permissions.create({
    'fileId': fileId,
    'transferOwnership': false,
    'resource': body
  });
  request.execute(function(resp) { 
      ...
  });

This will create a permission writer for that email address. After that, inside the request.execute() callback,

I send the second request:

var request2 = gapi.client.drive.permissions.list({
      'fileId': fileId
    });
    body.role = role;
    request2.execute(function(resp2) { 
      var request3 = gapi.client.drive.permissions.update({
        'fileId': fileId,
        'permissionId': resp2.permissions[1].id, //permission id of writer
        'transferOwnership': true,
        'resource': {'role':role, 'emailAddress': value}
      });
      request3.execute(function(resp3) {
        console.log(resp3);
      });
    });

In the above request, I used permissions.list to get the file permission id.

Then I used the permission id to update permissions. I used permissions.update request for transferOwnership. The problem I encountered here is "The user does not have sufficient permissions for this file."

What I am trying to do here is transfer the file ownership to an emailaddress. What is wrong with my code? How can I transfer file ownership?


Solution

  • You need to think about the old and new owners of the file. Unless they are in the same Google Accounts domain, (eg. [email protected] as opposed to [email protected]) you can't transfer ownership. Although it might sound inconvenient, there would be a clear security problem if this were to be allowed.

    If they are in the same domain, confirm that you have a scope with sufficient privilege.