使用PyDrive插入Google Drive文件的权限



我正在使用PyDrive在Google共享驱动器上创建Google Sheets文件,下面的代码片段成功地在我的共享驱动器文件夹中创建了该文件:

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似乎与创建的文件id相同。

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

感谢

我发现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)

我发现使用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)

最新更新