Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

python - Delete a file from different owner with Google Drive API

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': "[email protected]",
    '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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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".

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...