0
votes

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.

1
I have to apologize for my poor English skill. Unfortunately, from your question, I cannot understand about your goal. Can I ask you about the detail of your goal? - Tanaike
@Tanaike I am trying to use the google API to retrieve a folder's information using the folder's 32 character ID. For example, if I have a folder drive.google.com/drive/u/0/folders/…, I should be able to query using something like 'q="id = 1J5oHywhD9zDmOhgnfq6wdrrnyTJo6TX"' but it seems like "id" is not a valid query option. - Phantom of Krankor
Thank you for replying. From your replying, I proposed an answer. Could you please confirm it? If I misunderstood your question, I apologize. - Tanaike

1 Answers

1
votes

I believe your goal as follows.

  • You want to retrieve the folder information from the folder ID.
  • You want to achieve this using googleapis for python.

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.

Sample script:

service = build('drive', 'v3', credentials=creds)
folderId = '###' # Please set the folder ID.
res = service.files().get(fileId=folderId, fields='*').execute()
print(res)

Note:

  • 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)
    

References: