Python script using Drive API v2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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' store = file.Storage('storage.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets('client_secret.json', SCOPES) creds = tools.run_flow(flow, store, flags) \ if flags else tools.run(flow, store) SERVICE = build('drive', 'v2', http=creds.authorize(Http())) files = SERVICE.files().list().execute().get('items', []) for f in files: print (f['id'], f['mimeType']) |
Python script using Drive API v3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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' store = file.Storage('storage.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets('client_secret.json', SCOPES) creds = tools.run_flow(flow, store, flags) \ if flags else tools.run(flow, store) SERVICE = build('drive', 'v3', http=creds.authorize(Http())) files = SERVICE.files().list(fields='files(id, mimeType)').execute() for f in files.get('files', []): print (f.get('id'), f.get('mimeType')) |
Note,
The Python script for Drive API v3 was made with reference to the sample at the end of https://developers.google.com/drive/v3/web/search-parameters (see below).
The Result
The file IDs are on the left while the mimeType are on the left.
To save the output result into a local file, use the below command.
python listing_files_v3.py > out.txt
Listing your files in Google Drive
https://youtu.be/Z5G0luBohCg?list=PLOU2XLYxmsILOIxBRPPhgYbuSslr50KVq
No comments:
Post a Comment