Search code examples
google-apigoogle-drive-apigoogle-apps

How to update permissions in Google Drive API v3?


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': '[email protected]',
              'id': '00654354190098938408',
              'kind': 'drive#permission',
              'photoLink': 'https://lh6.googleusercontent.com/photo.jpg',
              'role': 'writer',
              'type': 'user'},
             {'deleted': False,
              'displayName': '[email protected]',
              'emailAddress': '[email protected]',
              '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": '[email protected]',
    "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.)""


Solution

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