Delete a file from different owner with Google Dri

2020-03-31 03:21发布

问题:

I'm trying to do a small application that reads a shared folder with different users of my organization, get the files and then delete them.

The problem is that I can't delete the file of a different user, because I can only delete files of my ownership (receiving a 403 Insufficient permissions for this file)

Another solution I found is change the owner of the file, but I get the same error.

I test it with a Native Application oAuth with a SuperAdmin account of the organization and with a service account, and none of them works.

A piece of my code trying to change the ownership:

new_permission = {
    'value': "admin@organization.com",
    'type': "user",
    'role': "writer"
}
perm = drive_service.permissions().insert(fileId=idfield, body=new_permission).execute()
perm['role'] = 'owner'
drive_service.permissions().update(fileId=idfield, permissionId=perm['id'], transferOwnership=True, body=perm).execute()

I spend hours searching and trying different solutions I found, but no one works (for example, Transfer ownership of a file fails - even as Super Administrator (Google Drive API, Java)).

Somebody has some idea? Thanks!

回答1:

The best solution right now involves two steps:

  1. As the admin account, determine the current owner for each file.
  2. Using delegated authorization, impersonate the owner of each file and transfer ownership to the admin account.

The email address of the owner is required to use delegated authorization, but isn't always visible in the permissions objects returned by the API. The email address will be visible if either:

  • The user has a Google+ account, their profile's "Contact Information" section includes their primary email address, and the email address's visibility is set to a level that includes the admin account.
  • The user is in a Google Apps domain and the domain has enabled contact sharing in the Admin console under "Google Apps > Contacts > Advanced settings".