Google Drive API with Python

Akshay Sharma
The HumAIn Blog
Published in
3 min readMay 25, 2021

A lot of companies are using Google Drive to manage and store some of their files so that their employees can access them easily from anywhere as long as there’s internet access. When I first started working with the API, I was having difficulties, and only after using it a few times that I developed a better understanding.

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 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.

In your terminal run the following to create and use a virtual environment.

$ virtualenv venv_google_api$ source venv_google_api/bin/activate

# install all the dependent packages

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.

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 the 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

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

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.

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.

Now, go to your terminal and execute the Python script.

There are a lot of other things that you can do with this. You can add a few code lines that accept 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.

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 metadata into analytics and see what can be done with this data.

Stay tuned for part II!

--

--