Search code examples
pythongoogle-drive-apigoogle-api-python-clientfile-listing

Google Drive python client / How to filter list of files with 'ownedByMe' and 'properties'?


I succeed to use the q parameter to filter files in Google Drive which names are not a given value, with following code.

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

I use the same service account to create these files and to list them.

I would then like to filter only the ones created by this service account. To do so, I tried using ownedByMe parameter, as listed in Google Documentation (Google API v3).

But I get following error.

    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">

Also, when trying to filter on properties, for instance with a key data_type, I get the following error.

    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">

Please, any idea what is wrong in my code? Thanks a lot for your help! Bests,


Solution

  • Unfortunately, in the current stage, there are no query of ownedByMe = true and properties['data_type'] = 'whatever'. I think that this is the reason of your issue of Invalid Value. So how about the following modification?

    Pattern 1:

    In this pattern, ownedByMe = true is achieved. In this case, please use the following search query.

    Modified search query:

    '###' in owners
    
    • In your case, ### is the email of service account.

    Pattern 2:

    In this pattern, properties['data_type'] = 'whatever' is achieved. In this case, please use the following search query.

    Modified search query:

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

    References: