【问题标题】:How to share a file from Google Drive with more than one user using Google Drive API for python?如何使用适用于 python 的 Google Drive API 与多个用户共享 Google Drive 中的文件?
【发布时间】:2021-06-12 20:54:40
【问题描述】:

我可以通过以下方式与给定用户共享文件:

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

但是,我不知道如何与多个用户共享文件。

我已阅读https://developers.google.com/drive/api/v3/reference/permissions/create 中的文档,并且我了解对于用户以外的用户,正文中指定的“类型”键的值必须是“组”而不是“用户”,但我没有找到在这种情况下,参数“emailAddress”的格式应该如何。我已经证明了一个邮件字符串列表,一个用逗号或空格分隔的邮件字符串,但这给了我以下错误:

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'}]">

【问题讨论】:

  • 如果您想与一组用户共享(这组用户不是整个域),您应该 create a group 与所有这些用户作为成员(这里是如何 manage a group programmatically 和这里如何到manage its members)。然后,emailAddress 将是组电子邮件。如果您不这样做,则必须分别为每个用户授予权限。
  • 感谢@lamblichus 的评论,我不知道 Google 群组的概念。

标签: python google-drive-api


【解决方案1】:

问题:

权限类型group 不涉及不同用户的任何组合。它指的是Google Group

解决办法:

如果您想与多个用户共享一个文件(这些用户不是一个完整的domain),您应该将所有这些用户都作为成员create a group

如果您有 Workspace 帐户,则可以使用Directory API 以编程方式管理组(请参阅Directory API: GroupsDirectory API: Group Members)。

在这种情况下,请求正文中的 emailAddress 将发送群组电子邮件:

如果typeusergroup,您必须为用户或组提供emailAddress

否则,唯一的选择是与不同的用户单独共享文件,就像您目前所做的那样。

参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    相关资源
    最近更新 更多