Reputation: 190
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
files().list()
query used to return 40 records of the 41 in the folder. Now it is only returning 39. files().get()
both of the non-returned folders still have the parent folder as their only parent, and their permissions have not changed.Main questions:
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?children()
method for folders, but it's been deprecated in v3 to my knowledgeUpvotes: 1
Views: 2258
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:
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