I modified the https://developers.google.com/drive/v2/reference/files/list example to get a list of all file items, using request partial resources.
def retrieve_all_files(service, fields = None):
"""Retrieve a list of File resources.
Args:
service: Drive API service instance.
fields: The fields (of each files Resource item!), e.g. 'id,title'
if not passed, get everything.
Returns:
List of File resources.
"""
result = []
page_token = None
while True:
try:
param = {}
if fields:
param['fields'] = 'nextPageToken,items(' + fields + ')'
if page_token:
param['pageToken'] = page_token
files = service.files().list(**param).execute()
result.extend(files['items'])
page_token = files.get('nextPageToken')
if not page_token:
break
except errors.HttpError, error:
print 'An error occurred: %s' % error
break
return result
In my test Drive folder I have a file that has a description string set. When I run
retrieve_all_files(drive_service)
without setting the fields parameter (so get everything), sure enough in the list of items, the file looks fine:
[{u'alternateLink': u'...',...
u'description': u'Description',
u'id': u'...',...},...]
but running
retrieve_all_files(drive_service, fields='id,description,title')
yields a list where none of the items have the 'description' property. Not all files have the 'description' property set; is this why the partial resource response is omitting the field even from items that have the property? I'm not sure if this is an undocumented feature that I didn't expect or a bug.