Google Drive API - A better way to get files you own

In the previous blog post about how to search your own files I used 'isAuthenticated' key to see if a file is mine (here). Today, I found a better and more accurate way to get files you own in Google Drive using the API:

+ Use the following query to search for files:

query = "'%s' in owners and trashed = false" % user_email

or

query = "'me' in owners and trashed = false"

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 BadStatusLine, badstatus:
            print 'Error when searching files: %s' % badstatus
            break
        except errors.HttpError, error:
            print 'Error when searching file: %s' % error
            break

    return result



+ Then, write a function to search our own files:

def get_own_files_by_email(service, user_email):
    query = "'%s' in owners and trashed = false" % user_email
    files = search_files(service, query)
    return files


def get_own_files(service):
    query = "'me' in owners and trashed =false"
    files = search_files(service, query)
    return files


Comments