How to update permissions in Google Drive API v3?

China☆狼群 提交于 2020-07-13 15:40:27

问题


I uploaded wav file using service account, service account created the file, service account is the owner:

metadata = {'name': filename, 'parents': [FolderId]}
media = MediaFileUpload(filepath, mimetype='audio/wav')
r = drive.files().create(body=metadata, media_body=media, fields='id').execute()

{'permissions': [{'deleted': False,
              'displayName': 'My name',
              'emailAddress': 'myemail@gmail.com',
              'id': '00654354190098938408',
              'kind': 'drive#permission',
              'photoLink': 'https://lh6.googleusercontent.com/photo.jpg',
              'role': 'writer',
              'type': 'user'},
             {'deleted': False,
              'displayName': 'myservice@myservice.iam.gserviceaccount.com',
              'emailAddress': 'myservice@myservice.iam.gserviceaccount.com',
              'id': '16815597635264162472',
              'kind': 'drive#permission',
              'role': 'owner',
              'type': 'user'}]}

Then I'm trying to change the role of myemail account from writer to owner and get an error:

drive.permissions().update(fileId=fileId, permissionId='00654354190098938408', transferOwnership=True,
    body={'role': 'owner'}).execute()

The user does not have sufficient permissions for this file.

What is wrong?

My service account is the owner, I CAN delete the file through the service account and it does not have permissions to transfer ownership, why? When I create a spreadsheet with service account and transfer ownership to myemail account it works perfectly.

I tried to create new permission and got this:

permission = {
    "emailAddress": 'myemail@gmail.com',
    "role": 'owner',
    "type": 'user',
}
drive.permissions().create(fileId=fileId, body=permission, transferOwnership=True).execute()

"Bad Request. User message: "You can't yet change the owner of this item. (We're working on it.)""


回答1:


As per the documentation found here Make someone else the owner of your file

From your personal account, you can transfer the following file types:

  • Google Docs
  • Google Sheets
  • Google Slides
  • Google Forms
  • Google Drawings
  • Google My Maps
  • Folders Tip: When you transfer ownership of a folder it transfers only the selected folder and does not include the files inside.

If google drive doesn't support transferring that file type then the service account isnt going to be able to transfer it either.

Workaround. Have the user upload the file to their account and not the service account.



来源:https://stackoverflow.com/questions/62699404/how-to-update-permissions-in-google-drive-api-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!