![](/img/trans.png)
[英]How can I create a new folder with Google Drive API in Python?
[英]How to create a folder in Team Drive with Google's Python Drive Client API?
我似乎找不到很多關於這方面的文章。 Google 的教程僅顯示在常規 Google Drive 文件夾中創建文件夾。
下面是我的 function 失敗並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.">
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 桌面應用程序,並且已被授權擁有完整的驅動器讀/寫 scope。
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']
我相信你的目標和情況如下。
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']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.