使用 Google 驱动器 API 创建文件夹:资源没有方法“插入”
Creating a folder using Google Drive API : Resource has no method "insert'
我想使用 Python API 在 google 驱动器上创建一个文件夹。
这是函数:
def create_folder(file_path, parentId=None):
body = {
'name': os.path.basename(file_path),
'mimeType': "application/vnd.google-apps.folder"
}
if parentId:
body['parents'] = parentId
results = service.files().insert(body=body).execute()
return results
但是报错:
AttributeError: 'Resource' object has no attribute 'insert'
我认为它会像这里的 get
和 list
方法一样工作-
https://developers.google.com/drive/v2/reference/files#methods
我在这里找到了答案:
How can I create a new folder with Google Drive API in Python?
我错过了什么?
您遇到“资源对象没有属性插入”,因为您正在使用 Google Drive API v3。 'files.insert' 方法仅适用于 Drive API v2。您可以使用“files.create”代替“files.insert”。
更多信息,请关注link:https://developers.google.com/drive/v3/web/migration
好像有两个问题:
- 当我执行你的代码时,一个文件夹被创建并且没有错误
信息。也许你可以 post 你初始化的代码
"service" 变量。如果驱动器的东西可能不是
已初始化,可能会失败。
- 要创建具有特定名称的文件夹,请使用 "title" 而不是 "name"。使用名称也可以,但文件夹将显示为 "New Folder"
我想使用 Python API 在 google 驱动器上创建一个文件夹。 这是函数:
def create_folder(file_path, parentId=None):
body = {
'name': os.path.basename(file_path),
'mimeType': "application/vnd.google-apps.folder"
}
if parentId:
body['parents'] = parentId
results = service.files().insert(body=body).execute()
return results
但是报错:
AttributeError: 'Resource' object has no attribute 'insert'
我认为它会像这里的 get
和 list
方法一样工作-
https://developers.google.com/drive/v2/reference/files#methods
我在这里找到了答案: How can I create a new folder with Google Drive API in Python?
我错过了什么?
您遇到“资源对象没有属性插入”,因为您正在使用 Google Drive API v3。 'files.insert' 方法仅适用于 Drive API v2。您可以使用“files.create”代替“files.insert”。
更多信息,请关注link:https://developers.google.com/drive/v3/web/migration
好像有两个问题:
- 当我执行你的代码时,一个文件夹被创建并且没有错误 信息。也许你可以 post 你初始化的代码 "service" 变量。如果驱动器的东西可能不是 已初始化,可能会失败。
- 要创建具有特定名称的文件夹,请使用 "title" 而不是 "name"。使用名称也可以,但文件夹将显示为 "New Folder"