Google云端硬盘python客户端/如何使用“ ownedByMe”和“ properties”过滤文件列表? 修改后的搜索查询:修改后的搜索查询:

问题描述

我成功使用q参数通过以下代码过滤了名称不是给定值的Google云端硬盘中的文件

    prefix = 'forbidden_name'
    res = files.list(
                     q="name != '" + prefix + "'",pageSize=800,pagetoken=None,fields='nextPagetoken,files(id,name,parents,properties,ownedByMe)').execute()

我使用相同的服务帐户来创建这些文件并列出它们。

然后,我只想过滤由该服务帐户创建的那些。 为此,我尝试使用Google Documentation (Google API v3)中列出的ownedByMe参数。

但是我得到以下错误

    res = files.list(
                     q="ownedByMe = true",ownedByMe)').execute()
HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files?q=ownedByMe+%3D+true&pageSize=800&fields=nextPagetoken%2C+files%28id%2C+name%2C+parents%2C+properties%2C+ownedByMe%29&alt=json returned "Invalid Value">

另外,当尝试使用键propertiesdata_type进行过滤时,出现以下错误

    res = files.list(
                     q="properties['data_type'] = 'whatever'",ownedByMe)').execute()
HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files?q=properties%5B%27data_type%27%5D+%3D+%27whatever%27&pageSize=800&fields=nextPagetoken%2C+files%28id%2C+name%2C+parents%2C+properties%2C+ownedByMe%29&alt=json returned "Invalid Value">

请问,我的代码有什么问题吗? 非常感谢你的帮助! 最好的

解决方法

不幸的是,在当前阶段,没有对ownedByMe = trueproperties['data_type'] = 'whatever'的查询。我认为这是您发出Invalid Value的原因。那么下面的修改如何?

模式1:

在这种模式下,实现ownedByMe = true。在这种情况下,请使用以下搜索查询。

修改后的搜索查询:

'###' in owners
  • 在您的情况下,###是服务帐户的电子邮件。

模式2:

在这种模式下,实现properties['data_type'] = 'whatever'。在这种情况下,请使用以下搜索查询。

修改后的搜索查询:

properties has {key='data_type' and value='whatever'}

参考文献: