【问题标题】:Inserting permissions to Google Drive file using PyDrive使用 PyDrive 向 Google Drive 文件插入权限
【发布时间】:2020-03-27 14:53:35
【问题描述】:

我正在使用 PyDrive 在 Google 共享驱动器上创建一个 Google 表格文件,下面的代码 sn-p 成功地在我的共享驱动器文件夹中创建了该文件:

f = gd.CreateFile(
            {'title': name, 'mimeType': 'application/vnd.google-apps.spreadsheet', 'parents': [{'teamDriveId': '1234', 'id': '1234'}]})
        f.Upload(param={'supportsTeamDrives': True})

在添加文件时,我还尝试设置电子邮件地址的权限以获得对文件的写入权限,如下所示:

f.InsertPermission({'type': 'user', 'value': 'myemail@email.com', 'role': 'writer'})

在尝试添加权限时,我收到以下错误:

<HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/file_id_here/permissions?alt=json returned "File not found: file_id_here">

我已检查,文件 ID 似乎与已创建的相同。

我认为如果我能够创建文件就不会出现任何授权问题?任何建议将不胜感激。

谢谢

【问题讨论】:

  • 你在使用https://www.googleapis.com/auth/drive.appdata范围吗?
  • 我使用默认的googleapis.com/auth/drive 范围,我认为它涵盖了所有内容,不是吗?
  • 确实如此,是的。您能否通过导航至https://docs.google.com/spreadsheets/d/file_id_here/edit#gid=0 确认 ID 100% 正确?
  • 是的,我可以确认我可以从该链接正常打开文件,因此 ID 看起来没问题。

标签: google-api google-drive-api google-oauth pydrive


【解决方案1】:

我发现这似乎是一个问题,在 InsertPermission 函数的 PyDrive 的 files.py 文件中没有“supportsTeamDrives”条目。

我在第 325 行的代码中添加了“supportsTeamDrives”参数,如下所示,现在它似乎可以工作了:

permission = self.auth.service.permissions().insert(
                fileId=file_id, body=new_permission, supportsTeamDrives=True).execute(http=self.http)

【讨论】:

    【解决方案2】:

    我发现使用 pyDrive 的最佳方法是这样做:

    file = gd.CreateFile({'title': 'filename', 'parents': [{'teamDriveId': '<your teamDriveId>', 'id': '<file id>'}]})
    file.Upload(param={'supportsTeamDrives': True})
    # here we can use the pydrive object to update.
    new_permission = {
        'id': 'anyoneWithLink',
        'type': 'anyone',
        'value': 'anyoneWithLink',
        'withLink': True,
        'role': 'reader'
    }
    permission = file.auth.service.permissions().insert(
        fileId=file['id'], body=new_permission, supportsTeamDrives=True).execute(http=file.http)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      相关资源
      最近更新 更多