【问题标题】:Google Drive API service account - file sharing within domainGoogle Drive API 服务帐户 - 域内文件共享
【发布时间】:2019-12-12 15:17:57
【问题描述】:

我正在尝试使用服务帐户管理特定域内的文件共享权限。

流程如下:

  1. 通过服务帐号授权

  2. 使用服务帐号创建文件夹

  3. 上传文件夹下的所有文件

  4. 在文件夹上插入权限

user_permission = {
    'value': issuer_email, (ex. user@company.com)
    'type': 'user',
    'role': 'writer'
}
drive_service.permissions().insert(fileId=f_id, body=user_permission, fields="id").execute()
domain_permission = {
     'type': 'domain',
     'role': 'writer',
     'domain': 'company.com'
}
drive_service.permissions().insert(fileId=f_id, body=domain_permission, fields="id").execute()

前 3 步进展顺利。但我没有收到步骤 (4) 的错误:

https://www.googleapis.com/drive/v2/files/1RqQUiSKP05ELbPX18YpcoqTGTG_RD2j4/permissions?fields=id&alt=json时HttpError 400返回“需要权限值字段”>

我尝试通过添加不同的字段来解决此错误,但没有成功。它还生成我想要的文件和文件夹,但文件夹和文件可以在公司域之外共享。 (在那里启用share 按钮,我可以从其他域访问它。)

那么如何限制域内的文件共享?就像在 Google Drive UI 提示分享一样,如果我选择了任何不合格的电子邮件地址,它应该无法分享。

谢谢!!!如果我什至不在正确的轨道上,请给我一些指导如何做到这一点。

更新

更具体地说,我想实现这一点:

使用第二个输入字段,虽然我可以与域外的 ppl 共享文件,但 ppl 需要请求许可才能访问它。 因为不是它不请求许可。

【问题讨论】:

  • 您为什么使用 Google Drive v2 而不是 Google Drive v3?

标签: google-api google-drive-api google-api-python-client


【解决方案1】:

看起来你有一些额外的代码可能会导致你的问题。尝试检查记录的示例

file_id = '1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ'
def callback(request_id, response, exception):
    if exception:
        # Handle error
        print exception
    else:
        print "Permission Id: %s" % response.get('id')

batch = drive_service.new_batch_http_request(callback=callback)
user_permission = {
    'type': 'user',
    'role': 'writer',
    'value': 'user@example.com'
}
batch.add(drive_service.permissions().insert(
        fileId=file_id,
        body=user_permission,
        fields='id',
))
domain_permission = {
    'type': 'domain',
    'role': 'reader',
    'value': 'example.com'
}
batch.add(drive_service.permissions().insert(
        fileId=file_id,
        body=domain_permission,
        fields='id',
))
batch.execute()

从 Drive v2 manage sharing 的文档中提取的代码

【讨论】:

  • 感谢@DalmTo!!!我尝试了上面的代码,它运行没有错误,但它没有授予正确的权限——我仍然可以与不在域中的 ppl 共享文件,他们不需要要求所有者访问文档——这不是我想了。我错过了什么吗?我正在用我想要的部分的图片编辑我上面的问题。谢谢!
  • 您不能授予对您不拥有的文件的访问权限。服务帐户需要拥有该文件才能授予其他人对该文件的权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多