Google Drive python 客户端 / 如何使用 'ownedByMe' 和 'properties' 过滤文件列表?

Google Drive python client / How to filter list of files with 'ownedByMe' and '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()

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

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

中所列

但是我得到以下错误。

    res = files.list(
                     q="ownedByMe = true",
                     pageSize=800, pageToken=None,
                     fields='nextPageToken, files(id, name, parents, properties, 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">

此外,当尝试过滤 properties 时,例如使用键 data_type,我收到以下错误。

    res = files.list(
                     q="properties['data_type'] = 'whatever'",
                     pageSize=800, pageToken=None,
                     fields='nextPageToken, files(id, name, parents, properties, 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
  • 在您的情况下,### 是服务帐户的电子邮件。

模式二:

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

修改后的搜索查询:

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

参考文献: