Thursday, December 5, 2013

Google Drive API - Search file in root folder

To search for files in root folder, using the following query:

query = "'root' in parents"

and then you can search with this function:

def search_files(service, query_string):
    result = []
    page_token = None
    while True:
        try:
            param = {}
            if page_token:
                param['pageToken'] = page_token
            param['q'] = query_string
            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