How to share a file from Google Drive with more than one user using Google Drive API for python?

Viewed 213

I am able to share a file with a given user by:

user_permission = ({
                    'type': 'user',
                    'role': 'writer',
                    'emailAddress': "example@gmail.com",
                   })
service.permissions().create(fileId=fileId,body=user_permission,fields='id').execute()

However, I don't know how to share the file with more than one user.

I have read the documentation in https://developers.google.com/drive/api/v3/reference/permissions/create, and I understand that for more than user the value of the "type" key specified in the body must be "group" instead of "user", but I don't find anywhere how the format of the parameter "emailAddress" should be in this case. I have proved with a list of strings of mails, a string of mails separated by commas or by spaces, but this has given me the following error:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files/1shxOQlNPqbXT-LwKTDvuwK0m-7IK43cDAWwxmnLgLvQ/permissions?fields=id&emailMessage=This+is+a+test&alt=json returned "The specified emailAddress is invalid or not applicable for the given permission type.". Details: "[{'domain': 'global', 'reason': 'invalid', 'message': 'The specified emailAddress is invalid or not applicable for the given permission type.', 'locationType': 'other', 'location': 'permission.emailAddress'}]">
1 Answers

Issue:

Permission type group does not refer to any combination of different users. It refers to a Google Group.

Solution:

If you want to share a file with multiple users (these users not being a whole domain) you should create a group with all these users as members.

If you have a Workspace account, you can manage a group programmatically, using Directory API (see Directory API: Groups and Directory API: Group Members).

In this case, the emailAddress from the request body would the group email:

if type is user or group, you must provide an emailAddress for the user or group

Otherwise, the only option is to share the file individually with the different users, as you are currently doing.

Reference:

Related