13

I am trying to retrieve file metadata from Google drive API V3 in Python. I did it in API V2, but failed in V3. I tried to get metadata by this line:

data = DRIVE.files().get(fileId=file['id']).execute()

but all I got was a dict of 'id', 'kind', 'name', and 'mimeType'. How can I get 'md5Checksum', 'fileSize', and so on?

I read the documentation. I am supposed to get all the metadata by get() methods, but all I got was a small part of it.

Here is my code:

from __future__ import print_function
import os

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive.metadata
https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('storage.json', scope=SCOPES)
    creds = tools.run_flow(flow, store)
DRIVE = build('drive','v3', http=creds.authorize(Http()))

files = DRIVE.files().list().execute().get('files',[])

for file in files:
    print('\n',file['name'],file['id'])
    data = DRIVE.files().get(fileId=file['id']).execute()
    print('\n',data)

print('Done')

I tried this answer: Google Drive API v3 Migration

List

Files returned by service.files().list() do not contain information now, i.e. every field is null. If you want list on v3 to behave like in v2, call it like this:

service.files().list().setFields("nextPageToken, files");

but I get a Traceback:

DRIVE.files().list().setFields("nextPageToken, files")
AttributeError: 'HttpRequest' object has no attribute 'setFields'
Kara
  • 6,115
  • 16
  • 50
  • 57
D47
  • 133
  • 1
  • 1
  • 6

3 Answers3

12

If you want to retrieve all the fields for a file resource, simply set fields='*'

In your above example, you would run

data = DRIVE.files().get(fileId=file['id'], fields='*').execute()

This should return all the available resources for the file as listed in: https://developers.google.com/drive/v3/reference/files

ScottMcC
  • 4,094
  • 1
  • 27
  • 35
  • This is great — how can I set a field? For instance, I'd like to run something like DRIVE.files().set(fileId=file['id'], fields={'writersCanShare': False}) but there's no set() method. I don't see a post() method either. Thoughts? – dslack Feb 07 '19 at 01:21
  • You can set a field using the `update` method. Check out this link https://developers.google.com/drive/api/v3/reference/files/update – ScottMcC Feb 11 '19 at 05:21
11

Suppose you want to get the md5 hash of a file given its fileId, you can do it like this:

DRIVE = build('drive','v3', http=creds.authorize(Http()))
file_service = DRIVE.files()
remote_file_hash = file_service.get(fileId=fileId, fields="md5Checksum").execute()['md5Checksum']

To list some files on the Drive:

results = file_service.list(pageSize=10, fields="files(id, name)").execute()

I have built a small application gDrive-auto-sync containing more examples of API usage.
It's well-documented, so you can have a look at it if you want.
Here is the main file containing all the code. It might look like a lot but more than half of lines are just comments.

Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77
0

There is a library PyDrive that provide easy interactions with google drive

https://googledrive.github.io/PyDrive/docs/build/html/filelist.html

Their example:

from pydrive.drive import GoogleDrive

drive = GoogleDrive(gauth) # Create GoogleDrive instance with authenticated GoogleAuth instance

# Auto-iterate through all files in the root folder.
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
  print('title: %s, id: %s' % (file1['title'], file1['id']))

All you need is file1['your key']

Yuan Fu
  • 191
  • 2
  • 15