pythongoogle-drive-apipydrive

Getting 'Invalid query' error when doing name='test' to Google Drive API


I am using PyDrive to fetch list of file names from a Google Drive folder

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = r"client_secrets.json"

gauth = GoogleAuth(settings_file='settings.yaml')

drive = GoogleDrive(gauth)

folder_id = "folder-id"

file_list = drive.ListFile(
{
    'q': "name = 'test'",
    'supportsAllDrives': True, 
    'includeItemsFromAllDrives': True, 
}
).GetList()

The syntax name = 'test' I saw from here

I am getting error

oogleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v2/files?q=name%3D%27test%27&maxResults=1000&alt=json returned "Invalid query". Details: "[{'domain': 'global', 'reason': 'invalid', 'message': 'Invalid query', 'locationType': 'parameter', 'location': 'q'}]">

If I fetch all file names, it works perfectly

'q': f"'{folder_id}' in parents and trashed=false"

My goal is to check if file with specific name exists or not in a particular folder, something like name = 'file-name' and 'folder-id' in parents and trashed=false


Solution

  • When I saw the script of pydrive, it seems that Drive API v2 is used. Ref In the case of Drive API v2, the metadata of filename is title. I thought that this might be the reason of your current issue of Invalid query. So, how about the following modification?

    From:

    'q': "name = 'test'",
    

    To:

    "q": "title = 'test'",
    

    Note:

    Reference: