Python 3-Google Drive API:AttributeError:“资源”对象没有属性“子级”

Alv*_*lve 5 python google-api google-drive-api google-api-python-client

我做了一个命令行文件夹选择器。我希望它列出文件夹中的所有文件。我已经尝试过使用service.children()-东西,但是我无法正常工作。无效的东西:

files = service.children().list(folderId=file_id).execute()
Run Code Online (Sandbox Code Playgroud)

这是代码实例化service对象:

service = build('drive', 'v3', http=creds.authorize(Http()))
Run Code Online (Sandbox Code Playgroud)

该代码的其他部分都起作用,所以我知道该服务正在起作用

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

teh*_*wch 6

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

通过Drive V3的Python客户端库文档提供了一些有用的信息:

about()返回关于资源。
changes()返回更改资源。
channels()返回渠道资源。
comments()返回评论资源。
files()返回文件资源。
permissions()返回权限资源。
replies()返回回复资源。
revisions()返回修订版资源。
teamdrives()返回teamdrives资源。根据发现文档
new_batch_http_request()创建一个BatchHttpRequest对象。

如果您不想迁移,则仍然有Drive V2children()资源:

about()返回关于资源。
apps()返回应用程序资源。
changes()返回更改资源。
channels()返回渠道资源。
children()返回子资源。
comments()返回评论资源。
files()返回文件资源。
parents()返回父资源。
permissions()返回权限资源。
properties()返回属性Resource。
realtime()返回实时资源。
replies()返回回复资源。
revisions()返回修订版资源。
teamdrives()返回teamdrives资源。根据发现文档
new_batch_http_request()创建一个BatchHttpRequest对象。

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

service = build('drive', 'v2', ...)
Run Code Online (Sandbox Code Playgroud)

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

您可以请求具有folderId正确参数的id的文件夹的子代,然后调用listand list_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)
Run Code Online (Sandbox Code Playgroud)

参考文献:

  • 在我的情况下,我必须在父母中使用“'{}'”(检查 {} 周围的'')。 (2认同)