gpt4 book ai didi

Python 3-谷歌驱动器 API : AttributeError: 'Resource' object has no attribute 'children'

转载 作者:太空狗 更新时间:2023-10-30 00:58:35 25 4
gpt4 key购买 nike

我做了一个命令行文件夹选择器。我希望它列出文件夹中的所有文件。我试过使用 service.children()-thing 但我无法让它工作。不起作用的地方:

files = service.children().list(folderId=file_id).execute()

这是实例化服务对象的代码:

service = build('drive', 'v3', http=creds.authorize(Http()))

代码的其他部分可以工作,所以我知道该服务正在工作

我知道变量 file_id 是一个有效的文件夹。谁知道它可能是?

最佳答案

您最近似乎将 API 版本从 2 升级到了 3!根据 Drive API changelog , 不再有 children() 资源。我怀疑还有其他您未预料到的更改,因此请务必查看该更改日志。

一些有用的信息来自 Drive V3 的 Python 客户端库文档:

about() Returns the about Resource.
changes() Returns the changes Resource.
channels() Returns the channels Resource.
comments() Returns the comments Resource.
files() Returns the files Resource.
permissions() Returns the permissions Resource.
replies() Returns the replies Resource.
revisions() Returns the revisions Resource.
teamdrives() Returns the teamdrives Resource.
new_batch_http_request() Create a BatchHttpRequest object based on the discovery document.

如果不想迁移,Drive V2还有一个children()资源| :

about() Returns the about Resource.
apps() Returns the apps Resource.
changes() Returns the changes Resource.
channels() Returns the channels Resource.
children() Returns the children Resource.
comments() Returns the comments Resource.
files() Returns the files Resource.
parents() Returns the parents Resource.
permissions() Returns the permissions Resource.
properties() Returns the properties Resource.
realtime() Returns the realtime Resource.
replies() Returns the replies Resource.
revisions() Returns the revisions Resource.
teamdrives() Returns the teamdrives Resource.
new_batch_http_request() Create a BatchHttpRequest object based on the discovery document.

那么,您的解决方案是构建 Drive REST API 的 V2 版本:

service = build('drive', 'v2', ...)

或继续使用 v3 并更新您的代码以使用现在需要的 files() 资源。

您可以使用适当的参数请求 ID 为 folderId 的文件夹的子文件夹,并调用 listlist_next:

Python3代码:

kwargs = {
"q": "'{}' in parents".format(folderId),
# Specify what you want in the response as a best practice. This string
# will only get the files' ids, names, and the ids of any folders that they are in
"fields": "nextPageToken,incompleteSearch,files(id,parents,name)",
# Add any other arguments to pass to list()
}
request = service.files().list(**kwargs)
while request is not None:
response = request.execute()
# Do stuff with response['files']
request = service.files().list_next(request, response)

引用资料:

关于Python 3-谷歌驱动器 API : AttributeError: 'Resource' object has no attribute 'children' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50705467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com