I’ve just started trying to use the Google Drive API. Using the quickstart guide I set up the authentication, I can print a list of my files and I can even make copies. All that works great, however I’m having trouble trying to access data from a file on Drive. In particular, I’m trying to get a WebViewLink
, however when I call .get
I receive only a small dictionary that has barely any of the file’s metadata. The documentation makes it look like all the data should just be there by default but it’s not appearing. I couldn’t find any way to flag for requesting any additional information.
credentials = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('drive', 'v3', http=http) results = service.files().list(fields="nextPageToken, files(id, name)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('Files:') for item in items: print(item['name'], item['id']) if "Target File" in item['name']: d = service.files().get(fileId=item['id']).execute() print(repr(d))
This is the output of the above code: (the formatting is my doing)
{u'mimeType': u'application/vnd.google-apps.document', u'kind': u'drive#file', u'id': u'1VO9cC8mGM67onVYx3_2f-SYzLJPR4_LteQzILdWJgDE', u'name': u'Fix TVP Licence Issues'}
For anyone confused about the code there is some missing that’s just the basic get_credentials
function from the API’s quickstart page and some constants and imports. For completeness, here’s all that stuff, unmodified in my code:
from __future__ import print_function import httplib2 import os from apiclient import discovery import oauth2client from oauth2client import client from oauth2client import tools SCOPES = 'https://www.googleapis.com/auth/drive' CLIENT_SECRET_FILE = 'client_secret.json' APPLICATION_NAME = 'Drive API Python Quickstart' try: import argparse flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() except ImportError: flags = None def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'drive-python-quickstart.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
So what’s missing, how can I get the API to return all that extra meta data that’s just not appearing right now?
Advertisement
Answer
You are very close. With the newer version of the Drive API v3, to retrieve other metadata properties, you will have to add the fields
parameter to specify additional properties to include in a partial response.
In your case, since you are looking to retrieve the WebViewLink
property your request should look something similar to this:
results = service.files().list( pageSize=10,fields="nextPageToken, files(id, name, webViewLink)").execute()
To display your items from the response:
for item in items: print('{0} {1} {2}'.format(item['name'], item['id'], item['webViewLink']))
I also suggest try it out with the API Explorer so you can view what additional metadata properties you would like to display on your response.
Good Luck and Hope this helps ! :)