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

Question:

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

Asked By: Umair Ayub

||

Answers:

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:

  • About 'q': f"'{folder_id}' in parents and trashed=false", in this case, both Drive API v2 and v3 can be used. By this, I think that no error occurs.

Reference:

Answered By: Tanaike
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.