Warm tip: This article is reproduced from serverfault.com, please click

其他-如何使用Google的Python Drive Client API在Team Drive中创建文件夹?

(其他 - How to create a folder in Team Drive with Google's Python Drive Client API?)

发布于 2020-12-04 17:45:49

我似乎在这方面找不到很多文章。Google的教程仅显示在常规Google云端硬盘文件夹中创建文件夹。

下面是我的功能,它失败了 oogleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files?alt=json returned "File not found: myteamdrivefolderid.". Details: "File not found: myteamdrivefolderid.">

我的应用程序是Python桌面应用程序,并且已经被授权具有完整的云端硬盘读/写范围。

def create_folder(service, name, parent_id=None, **kwargs):
    # Create a folder on Drive, returns the newely created folders ID
    body = {
        'name': name,
        'mimeType': "application/vnd.google-apps.folder"
    }
    if parent_id:
        body['parents'] = [parent_id]
    if 'teamDriveId' in kwargs:
        body['teamDriveId'] = kwargs['teamDriveId']
    folder = service.files().create(body=body).execute()
    return folder['id']
Questioner
fhcat
Viewed
11
Tanaike 2020-12-05 07:08:28

我相信你的目标和情况如下。

  • 你要使用googleapis for python在共享云端硬盘中创建新文件夹。
  • 你具有在共享驱动器中创建新文件夹的权限。
  • 你已经可以使用Drive API。

修改要点:

  • 在这种情况下,请添加supportsAllDrives到查询参数。
  • 似乎teamDriveId已弃用。请使用driveId参考

当以上几点反映到你的脚本时,它将变为以下内容。

修改后的脚本:

body = {
    'name': name,
    'mimeType': "application/vnd.google-apps.folder"
}
if parent_id:
    body['parents'] = [parent_id]
if 'teamDriveId' in kwargs:
    body['driveId'] = kwargs['teamDriveId'] # Modified
folder = service.files().create(body=body, supportsAllDrives=True).execute() # Modified
return folder['id']

参考: