William Daly
William Daly

Reputation: 190

Certain files missing in Google Drive API v3 Python files().list()

I'm new to using the Google Drive API for Python (v3) and I've been trying to access and update the sub-folders in a particular parent folder for which I have the fileId. Here is my build for the API driver:

store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json',
           scope='https://www.googleapis.com/auth/drive')
    creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))

I am able to successfully access most of the sub-folders by using files().list() but at least one was missing from the list of results returned:

results = service.files().list(
    q="parents in '1QXPl6z04GsYAO0GKHBk2oBjEweaAbczw'", 
    fields="files(id, name), incompleteSearch, nextPageToken").execute()
items = results['files']

I double checked and there was no nextPageToken key in the results and the value of incompleteSearch was False, which I assume means the full list of results were returned. In addition when I accessed the list of parents for the missing file by using the file().get() method, the only parent listed is the one in the query above:

service.files().get(
    fileId='1WHP02DtXfJHfkdr47xSeeRIj0sCrihPA',
    fields='parents, name').execute()

and returns this:

{'name': 'Sara Gaul -Baltimore Corps docs and schedules',
 'parents': ['1QXPl6z04GsYAO0GKHBk2oBjEweaAbczw']}

Other details that may be relevant:

UPDATES

Main questions:

  1. Why isn't this file which clearly has the parent id listed in my file().list() query showing up in the results of that query? And is there any way to adjust the query or the file to ensure that it does?
  2. Is there an easier way to list all of the files contained within a folder in the Google Drive API v3? I know that v2 had a children() method for folders, but it's been deprecated in v3 to my knowledge

Upvotes: 1

Views: 2258

Answers (1)

William Daly
William Daly

Reputation: 190

I figured out the error with my code:

My previous query parameter in the files().list() method was:

results = service.files().list(
    q="parents in '1QXPl6z04GsYAO0GKHBk2oBjEweaAbczw'", 
    fields="files(id, name), incompleteSearch, nextPageToken").execute()
items = results['files']

After looking at another bug someone had posted in Google's issue tracker for the API, I saw the preferred syntax for that query was:

results = service.files().list(
    q="'1QXPl6z04GsYAO0GKHBk2oBjEweaAbczw' in parents", 
    fields="files(id, name), incompleteSearch, nextPageToken").execute()
items = results['files']

In other words switching the order of parents in fileId to fileId in parents. With the resulting change in syntax all 41 files were returned.

I have two follow-up questions that hopefully someone can clarify:

  1. Why would the first syntax return any records at all if it is incorrect? And why would changing the name of a file prevent it from being returned using the first syntax?
  2. If you wanted to return a list of files that were stored in one of a few folders, is there any way to pass multiple parent ids to the query as the parents in ... syntax would suggest? Or do they have to be evaluated as separate conditions i.e. fileId1 in parents or fileId2 in parents?

If someone could comment on this answer with those explanations or post a more complete answer, I would gladly select it as the best response.

Upvotes: 4

Related Questions