Reputation: 105
Is there a way to have the Google API search by ID? I have tried a number of combinations of q="id='FOLDER_ID'" (I have been able to lookups by name, so I know my overall code is working), but I invariably get an error like this:
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files?q=id%3D%27FOLDER_ID%27&spaces=drive&fields=%2A&alt=json returned "Invalid Value". Details: "[{'domain': 'global', 'reason': 'invalid', 'message': 'Invalid Value', 'locationType': 'parameter', 'location': 'q'}]">
I haven't been able to find any documentation for the service.files().list() functions (all I found was "supported syntax" which is totally unhelpful), nor can I find an example for what I would have thought was a fairly basic lookup.
I'm doing this in python, but that's probably just secondary.
Upvotes: 1
Views: 1554
Reputation: 201378
I believe your goal as follows.
At the method of "Files: list", when the folder ID is used, the file list under the folder of folder ID can be retrieved. From your question, I thought that you might have wanted to use 'folderId' in parents
as the search query. In the current stage, id
is not used in the search query. For example, when name='folder name'
of the search query is used, the folder information can be retrieved. Unfortunately, the folder information cannot be directly retrieved from the folder ID using the method of "Files: list".
In this case, I think that you can use the method of "Files: get" can be used. When this is reflected to the script using googleapis for python, it becomes as follows.
service = build('drive', 'v3', credentials=creds)
folderId = '###' # Please set the folder ID.
res = service.files().get(fileId=folderId, fields='*').execute()
print(res)
If you want to achieve your goal using the method of "Files: list" and the folder ID, you can also use the folloing script. In this case, at first, the folder name is retrieved using the method of "Files: get" and the folder information is retrieved using the method of "Files: list". The search query of q="name='" + res1['name'] + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"
is used for this. In this case, when the same folder names are existing in the Drive, the "Files: list" returns those folder information.
service = build('drive', 'v3', credentials=creds)
folderId = '###' # Please set the folder ID.
res1 = service.files().get(fileId=folderId).execute()
res2 = service.files().list(q="name='" + res1['name'] + "' and mimeType='application/vnd.google-apps.folder' and trashed=false", fields='*').execute()
print(res2)
Upvotes: 2