Python download file from google drive - opinion you
If you have not set up your Google Drive API, the scope and credentials please follow this tutorial here.
In order to be able to access the API resources, we need to get the access token. Since the API resources that we want to access contain sensitive scope (accessing files in Google Drive), we need to do a browser authentication for the first time.
Before we begin, let’s set up a to contain and install all the dependencies for this project. We are going to start with installing and libraries.
MacBook-Pro:~ bobthedude$ virtualenv venv_google_apiMacBook-Pro:~ bobthedude$ source venv_google_api/bin/activate# install all the dependent packages
(venv_google_api) MacBook-Pro:~ bobthedude$ pip install google-api-python-client oauth2client
Create a folder in the project directory called to store your which you can download from your Google Console. If you don’t know how to do this, follow this tutorial here.
Your project structure should look like this to begin with. The is the Python file that contains instructions to connect to Google Drive API.
first_medium_project|
|__credentials
| |
| |__client_www.cronistalascolonias.com.ar
|
|__connect_to_google_www.cronistalascolonias.com.ar
Let’s write the code that needs to go into .
from apiclient import discoveryfrom httplib2 import Http
from oauth2client import client, file, tools
# define path variables
credentials_file_path = './credentials/www.cronistalascolonias.com.ar'
clientsecret_file_path = './credentials/client_www.cronistalascolonias.com.ar'
# define API scope
SCOPE = 'www.cronistalascolonias.com.ar'
# define store
store = www.cronistalascolonias.com.are(credentials_file_path)
credentials = www.cronistalascolonias.com.ar()# get access token
if not credentials or www.cronistalascolonias.com.ard:
flow = www.cronistalascolonias.com.ar_from_clientsecrets(clientsecret_file_path, SCOPE)
credentials = www.cronistalascolonias.com.ar_flow(flow, store)
Your access token will be stored in upon the browser authentication, which I will go through after this.
Now, save the file and go to your . Follow below commands to get your access token. Note: if you execute the above script from an IDE (I use ), you would get an error. For the first time only, you need to execute the script from your .
(venv_google_api) MacBook-Pro:~ bobthedude$ cd first_medium_project(venv_google_api) MacBook-Pro:first_medium_project bobthedude$ python connect_to_google_www.cronistalascolonias.com.ar# below is the output of the above command
/Users/bobthedude/virtualenv/venv_google_api/lib/python/site-packages/oauth2client/_www.cronistalascolonias.com.ar UserWarning: Cannot access ./credentials/www.cronistalascolonias.com.ar: No such file or directory
www.cronistalascolonias.com.ar(_MISSING_FILE_www.cronistalascolonias.com.ar(filename))Your browser has been opened to visit:www.cronistalascolonias.com.ar?client_id=www.cronistalascolonias.com.ar&redirect_uri=http%3A%2F%2Flocalhost%3A%2F&scope=https%3A%2F%www.cronistalascolonias.com.ar%2Fauth%2Fdrive&access_type=offline&response_type=codeIf your browser is on a different machine then exit and re-run this
application with the command-line parameter--noauth_local_webserver
A browser tab will open after the above command execution. Select the Google account on which you set up your API and credentials in the previous step. Then click on Allow.
Once again, Google will prompt you and ask for a confirmation if you want to allow your First Medium Application to access your Google Drive files. Go on and click on Allow.
If successful, your browser will return a page with the following text . And if you go back to your , you should see an additional 2 lines (indicating authentication success) printed as follows.
/Users/bobthedude/virtualenv/venv_google_api/lib/python/site-packages/oauth2client/_www.cronistalascolonias.com.ar UserWarning: Cannot access ./credentials/www.cronistalascolonias.com.ar: No such file or directorywww.cronistalascolonias.com.ar(_MISSING_FILE_www.cronistalascolonias.com.ar(filename))Your browser has been opened to visit:www.cronistalascolonias.com.ar?client_id=www.cronistalascolonias.com.ar&redirect_uri=http%3A%2F%2Flocalhost%3A%2F&scope=https%3A%2F%www.cronistalascolonias.com.ar%2Fauth%2Fdrive&access_type=offline&response_type=codeIf your browser is on a different machine then exit and re-run this
application with the command-line parameter--noauth_local_webserverAuthentication successful.
success
Additionally, you will see a new file gets created in the folder. Awesome job! We have now obtained our access token which we can use to connect to Google Drive API resources.
(venv_google_api) MacBook-Pro:first_medium_project bobthedude$ ls credentials/# output as belowclient_www.cronistalascolonias.com.ar www.cronistalascolonias.com.ar
Now, let’s add a function to our that would retrieve all the files that we have in our Google Drive.
from apiclient import discovery, errorsfrom httplib2 import Http
from oauth2client import client, file, tools
# define variables
credentials_file_path = './credentials/www.cronistalascolonias.com.ar'
clientsecret_file_path = './credentials/client_www.cronistalascolonias.com.ar'
# define scope
SCOPE = 'www.cronistalascolonias.com.ar'
# define store
store = www.cronistalascolonias.com.are(credentials_file_path)
credentials = www.cronistalascolonias.com.ar()
if not credentials or www.cronistalascolonias.com.ard:
flow = www.cronistalascolonias.com.ar_from_clientsecrets(clientsecret_file_path, SCOPE)
credentials = www.cronistalascolonias.com.ar_flow(flow, store)
# define API service
http = www.cronistalascolonias.com.arize(Http())
drive = www.cronistalascolonias.com.ar('drive', 'v3', http=http)
# define a function to retrieve all files
def retrieve_all_files(api_service):
results = []
page_token = None
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
files = api_www.cronistalascolonias.com.ar().list(**param).execute() # append the files from the current result page to our list
www.cronistalascolonias.com.ar(www.cronistalascolonias.com.ar('files')) # Google Drive API shows our files in multiple pages when the number of files exceed
page_token = www.cronistalascolonias.com.ar('nextPageToken')
if not page_token:
break
except www.cronistalascolonias.com.arror as error:
print(f'An error has occurred: {error}')
break # output the file metadata to console
for file in results:
print(file)
return results
# call the function
all_files = retrieve_all_files(drive)
Now, go to your and execute the Python script.
(venv_google_api) MacBook-Pro:first_medium_project bobthedude$ python connect_to_google_www.cronistalascolonias.com.ar# output will look like this{'kind': 'drive#file', 'id': 'TyVhFjlzOhL9MoazYiFbyABWgV9abC', 'name': 'Christmas Plan', 'mimeType': 'application/www.cronistalascolonias.com.arsheet'}
{'kind': 'drive#file', 'id': 'sN8zlbWAptpqNOgEihTZhgKgsAbac9', 'name': 'Event Feedback', 'mimeType': 'application/www.cronistalascolonias.com.ar'}
{'kind': 'drive#file', 'id': 'URWSjdpbEABCDE', 'name': 'Workshop ', 'mimeType': 'application/www.cronistalascolonias.com.ar'}
You can see from the above, our Python script has successfully connected to the Google Drive API resources, retrieved all the files and printed the file metadata.
There’s a lot of other things that you can do with this. You can add a few lines of code that accepts a file name so that your Python script will retrieve the file metadata for a specific file that you are interested in. Let’s modify the function and see how this might look like.
# define a function to retrieve all filesdef retrieve_all_files(api_service, filename_to_search):
results = []
page_token = None
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
files = api_www.cronistalascolonias.com.ar().list(**param).execute()# append the files from the current result page to our list
www.cronistalascolonias.com.ar(www.cronistalascolonias.com.ar('files'))# Google Drive API shows our files in multiple pages when the number of files exceed
page_token = www.cronistalascolonias.com.ar('nextPageToken')
if not page_token:
break
except www.cronistalascolonias.com.arror as error:
print(f'An error has occurred: {error}')
break# output the file metadata to console
for file in results:
if www.cronistalascolonias.com.ar('name') == filename_to_search:
print(file)
break
return results, file
# call the function
filename_to_search = 'Christmas Plan'
all_files, search_file = retrieve_all_files(drive, filename_to_search)
In the next part of this tutorial series, I am going to show you how we can download a specific worksheet from a Google sheet into a csv file. Also, we will transform the code above into a command line application, rather than just a plain Python script.
You can see the first part of this tutorial here.
Stay tuned for part III! 😃
-