Python 3 - Google Drive API: AttributeError: 'Resource' object has no attribute 'children'

Python 3 - Google Drive API: AttributeError: 'Resource' object has no attribute 'children'

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

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

这是实例化 service 对象的代码:

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

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

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

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

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

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 的 V2 版本 API:

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)

参考文献: