【问题标题】:Python 3 - Google Drive API: AttributeError: 'Resource' object has no attribute 'children'Python 3 - Google Drive API:AttributeError:'Resource'对象没有属性'children'
【发布时间】:2018-06-05 17:05:55
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: python google-api google-drive-api google-api-python-client


    【解决方案1】:

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

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

    about() 返回 about 资源。
    changes() 返回更改资源。
    channels() 返回渠道资源。
    comments() 返回 cmets 资源。
    files()返回文件资源。
    permissions() 返回权限资源。
    replies() 返回回复资源。
    revisions() 返回修订资源。
    teamdrives() 返回团队驱动资源。new_batch_http_request()根据发现文档创建BatchHttpRequest对象。

    如果你不想迁移,children() 资源还有Drive V2

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

    那么,您的解决方案是构建 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)
    

    参考资料:

    【讨论】:

    • 在我的情况下,我不得不在父母中使用“'{}'”(在 {} 周围检查 '')。
    • 根据examplefolderId 必须用引号引起来
    猜你喜欢
    • 2019-02-26
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多