2016/10/26

PyDrive Quick Overview - Authentication and File Listing - PyDrive vs Drive API Python

I came across PyDrive (https://pythonhosted.org/PyDrive/index.html) while trying to learn how to use Python to access Google Drive via Google Drive API. For someone struggling to understand how to use the Google Drive API, I think it's a godsend to have PyDrive..

OAuth2 authentication

Using PyDrive

1
2
3
4
from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.


Using Drive API v3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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()))

Listing All Files On Google Drive

Using PyDrive

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.

from pydrive.drive import GoogleDrive

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

# Auto-iterate through all files on Google Drive.
file_list = drive.ListFile().GetList()
for file1 in file_list:
   print('id: %s, mimeType: %s' % (file1['id'], file1['mimeType']))

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('id: %s, mimeType: %s' % (f.get('id'), f.get('mimeType')))

Listing All Files In The Root Folder

Using PyDrive

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.

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('name: %s, id: %s, mimeType: %s' % (file1['title'], file1['id'], file1['mimeType']))

Note the name for the filename field is "title" not "name" (I guess PyDrive is using Drive API v2 because name is called title in API v2).

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(q="'root' in parents and trashed=false",fields='files(name, id, mimeType)').execute()
for f in files.get('files', []):
 print('name: %s, id: %s, mimeType: %s' % (f.get('name'), f.get('id'), f.get('mimeType')))

Paginate and iterate through files

Using PyDrive

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.

from pydrive.drive import GoogleDrive

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

# Paginate file lists by specifying number of max results
for file_list in drive.ListFile({'q': 'trashed=true', 'maxResults': 10}):
  print('Received %s files from Files.list()' % len(file_list)) # <= 10
  for file1 in file_list:
      print('title: %s, id: %s' % (file1['title'], file1['id']))

Using Drive API v3

The last part of the code was made with reference to Python Quickstart at
https://developers.google.com/drive/v3/web/quickstart/python.

 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
26
27
28
29
30
31
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()))

page_token = None
while True:
 files = SERVICE.files().list(pageSize=10,q="trashed=true",fields='files(id, name), nextPageToken',pageToken=page_token).execute()
 print('Received files from files.list()')
 for f in files.get('files', []):
   print('name: %s, id: %s' % (f.get('name'), f.get('id')))
 page_token = files.get('nextPageToken', None)
 if page_token is None:
    break;

--------------------------------------------------------------------------------------------------------------------------
Drive API v3 Search Parameters

The code below is for Drive API v3. 3 different search parameters are used in Line 25 ~ 32 to form different queries. For more info., visit https://developers.google.com/drive/v3/web/search-parameters.

 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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()))

page_token = None
while True:
  #find files that are shared with me
 #files = SERVICE.files().list(q="sharedWithMe",fields='files(id, name), nextPageToken',pageToken=page_token).execute()

  #find files that are in the root folder
 #files = SERVICE.files().list(q="'root' in parents",fields='files(id, name), nextPageToken',pageToken=page_token).execute()

  #find files that containing the word "hello" in the content
 files = SERVICE.files().list(q="fullText contains 'hello'",fields='files(id, name), nextPageToken',pageToken=page_token).execute()
 
 print('Received files from files.list()')
 for f in files.get('files', []):
   print('name: %s, id: %s' % (f.get('name'), f.get('id')))
 page_token = files.get('nextPageToken', None)
 if page_token is None:
    break;

Reference:

Google Drive SDK: Searching for files
https://youtu.be/DOSvQmQK_HA?list=PL0FA2818902D9D123



No comments:

Post a Comment