Google drive api python documentation

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.

Google drive api python documentation

Before we begin, let’s set up a virtual environment to contain and install all the dependencies for this project. We are going to start with installing google-api-python-client and oauth2client libraries.

MacBook-Pro:~ bobthedude$ virtualenv venv_google_api
MacBook-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 credentials to store your client_secret.json 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 connect_to_google_drive.py is the Python file that contains instructions to connect to Google Drive API.

first_medium_project
|
|__credentials
| |
| |__client_secret.json
|
|__connect_to_google_drive.py

Let’s write the code that needs to go into connect_to_google_drive.py.

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

# define path variables
credentials_file_path = './credentials/credentials.json'
clientsecret_file_path = './credentials/client_secret.json'

# define API scope
SCOPE = 'https://www.googleapis.com/auth/drive'

# define store
store = file.Storage(credentials_file_path)
credentials = store.get()

# get access token
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(clientsecret_file_path, SCOPE)
credentials = tools.run_flow(flow, store)

Your access token will be stored in credentials/credentials.json upon the browser authentication, which I will go through after this.

Now, save the file and go to your terminal. Follow below commands to get your access token. Note: if you execute the above script from an IDE (I use PyCharm), you would get an error. For the first time only, you need to execute the script from your terminal.

(venv_google_api) MacBook-Pro:~ bobthedude$ cd first_medium_project
(venv_google_api) MacBook-Pro:first_medium_project bobthedude$ python connect_to_google_drive.py
# below is the output of the above command
/Users/bobthedude/virtualenv/venv_google_api/lib/python3.7/site-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access ./credentials/credentials.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Your browser has been opened to visit:https://accounts.google.com/o/oauth2/auth?client_id=123456789101-ab12r1abcdefjklm1a2bcdefghi12a1a.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%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.

Google drive api python documentation

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.

Google drive api python documentation

If successful, your browser will return a page with the following text The authentication flow has completed. And if you go back to your terminal, you should see an additional 2 lines (indicating authentication success) printed as follows.

/Users/bobthedude/virtualenv/venv_google_api/lib/python3.7/site-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access ./credentials/credentials.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Your browser has been opened to visit:https://accounts.google.com/o/oauth2/auth?client_id=123456789101-ab12r1abcdefjklm1a2bcdefghi12a1a.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%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 credentials.json gets created in the credentials 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 below
client_secret.json credentials.json

Now, let’s add a function to our connect_to_google_drive.py that would retrieve all the files that we have in our Google Drive.

from apiclient import discovery, errors
from httplib2 import Http
from oauth2client import client, file, tools

# define variables
credentials_file_path = './credentials/credentials.json'
clientsecret_file_path = './credentials/client_secret.json'

# define scope
SCOPE = 'https://www.googleapis.com/auth/drive'

# define store
store = file.Storage(credentials_file_path)
credentials = store.get()

if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(clientsecret_file_path, SCOPE)
credentials = tools.run_flow(flow, store)

# define API service
http = credentials.authorize(Http())
drive = discovery.build('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_service.files().list(**param).execute()

# append the files from the current result page to our list
results.extend(files.get('files'))
# Google Drive API shows our files in multiple pages when the number of files exceed 100
page_token = files.get('nextPageToken')

if not page_token:
break

except errors.HttpError 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 terminal and execute the Python script.

(venv_google_api) MacBook-Pro:first_medium_project bobthedude$ python connect_to_google_drive.py# output will look like this
{'kind': 'drive#file', 'id': '12345678901234TyVhFjlzOhL9MoazYiFbyABWgV9abC', 'name': 'Christmas Plan', 'mimeType': 'application/vnd.google-apps.spreadsheet'}
{'kind': 'drive#file', 'id': '12345678901234sN8zlbWAptpqNOgEihTZhgKgsAbac9', 'name': 'Event Feedback', 'mimeType': 'application/vnd.google-apps.form'}
{'kind': 'drive#file', 'id': '12345678901234URWSjdpbEABCDE', 'name': 'Workshop 2010', 'mimeType': 'application/vnd.google-apps.folder'}

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 retrieve_all_files function and see how this might look like.

# define a function to retrieve all files
def 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_service.files().list(**param).execute()

# append the files from the current result page to our list
results.extend(files.get('files'))
# Google Drive API shows our files in multiple pages when the number of files exceed 100
page_token = files.get('nextPageToken')

if not page_token:
break

except errors.HttpError as error:
print(f'An error has occurred: {error}')
break

# output the file metadata to console
for file in results:
if file.get('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! 😃

How do I use Google Docs API in Python?

A Google account..
Step 1: Install the Google client library. To install the Google client library for Python, run the following command: pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib. ... .
Step 2: Configure the sample. To configure the sample: ... .
Step 3: Run the sample. To run the sample:.

How does Python read data from Google Drive?

1) Install PyDrive. The first step is to install PyDrive. ... .
2) Authenticate. The second step is to authenticate and create a PyDrive client..
3) Authorizing. ... .
4) Generating a shareable link. ... .
5) Getting the file_id. ... .
6) Load the CSV. ... .
7) Showing the Results..

Can I run Python on Google Drive?

You can use the powerful and popular Python language in your Google Drive, and the set-up will take less than five minutes.

Is there an API for Google Drive?

The Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Drive, and create robust functionality in your application using the Drive API.