Top 10 Google Drive API Python Solutions for Developers

Jennie Lee
5 min readMar 26, 2024

--

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction to Google Drive API and Python

Google Drive is a widely-used cloud storage service that allows users to store, share, and access files from anywhere with an internet connection. It offers free storage and paid plans for additional storage capacity. With the increasing popularity of cloud storage, developers often need to integrate Google Drive functionality into their applications to provide users with seamless file management. This is where the Google Drive API comes in handy.

The Google Drive API provides a set of methods and tools that developers can use to interact with Google Drive programmatically. It allows developers to create, read, update, and delete files and folders within a user’s Google Drive account. By utilizing the API, developers can automate file operations, synchronize files across multiple devices, and integrate Google Drive with their own applications.

In this article, we will focus on using Python to interact with the Google Drive API. Python is a versatile programming language that is widely used by developers for its simplicity and readability. It has a rich ecosystem of libraries and frameworks that make it a popular choice for various tasks, including working with APIs.

Now, let’s dive into the step-by-step process of setting up a project and utilizing the Google Drive API with Python.

Setting Up the Project

The first step in utilizing the Google Drive API is to create a new project on the Google Cloud Dashboard. The Google Cloud Dashboard provides a centralized platform for managing your projects, APIs, credentials, and more.

To create a new project, follow these steps:

  1. Go to the Google Cloud Dashboard at https://console.cloud.google.com/
  2. Click on the project dropdown at the top of the page and then click on “New Project”.
  3. In the “New Project” dialog, enter a name for your project and click on the “Create” button.

Once the project is created, you will be redirected to the project dashboard. From here, you can enable the Google Drive API for your project.

To enable the Google Drive API, follow these steps:

  1. On the left sidebar, click on the “APIs & Services” menu and then click on “Library”.
  2. In the search bar, type “Google Drive API” and click on the “Google Drive API” result.
  3. Click on the “Enable” button to enable the API for your project.

Congratulations! You have successfully set up your project and enabled the Google Drive API.

Creating Credentials

To interact with the Google Drive API, you need to create credentials that will authenticate your application and allow it to access a user’s Google Drive account.

To create credentials, follow these steps:

  1. On the left sidebar, click on the “APIs & Services” menu and then click on “Credentials”.
  2. Click on the “Create Credentials” button and select “Service account” from the dropdown.
  3. Enter a name for your service account and click on the “Create” button.

Once you have created the service account, you need to give it the necessary access to your Google Drive account.

To grant access to your Google Drive account, follow these steps:

  1. Click on the newly created service account email address.
  2. Click on the “Add Key” button and select “Create new JSON key”.
  3. Click on the “Create” button to download the JSON key file.

Rename the downloaded JSON key file to “credentials.json” and place it in your project directory. This file will be used to authenticate your application when accessing the Google Drive API.

Creating and Sharing Folders

Now that you have set up your project and created the necessary credentials, you can proceed to create and share folders within your Google Drive account.

To create a new folder, follow these steps:

  1. Go to your Google Drive account and click on the “New” button.
  2. Select “Folder” from the dropdown menu.
  3. Enter a name for your folder and click on the “Create” button.

Once you have created the folder, note down the ID from the URL. The ID is a unique identifier for the folder and will be used in the script to interact with it.

To share the folder with the service account email address, follow these steps:

  1. Right-click on the folder and select “Share”.
  2. In the “People” field, enter the service account email address.
  3. Choose the appropriate permissions for the service account and click on the “Send” button.

Interacting with Google Drive Using Python

Now that you have set up your project, created the necessary credentials, and created and shared a folder within your Google Drive account, you can start interacting with Google Drive using Python.

Below is a sample Python code that demonstrates how to create a folder, upload a file to the folder, and list the files inside the folder using the Google Drive API:

import os
import io
import json
import pickle
from googleapiclient.discovery import build
from google.oauth2 import service_account
from googleapiclient.http import MediaFileUpload


# Load credentials from "credentials.json" file
creds = None
if os.path.exists('credentials.json'):
creds = service_account.Credentials.from_service_account_file(
'credentials.json',
scopes=['https://www.googleapis.com/auth/drive']
)


# Create a new folder
def create_folder(folder_name):
service = build('drive', 'v3', credentials=creds)
folder_metadata = {
'name': folder_name,
'mimeType': 'application/vnd.google-apps.folder'
}
folder = service.files().create(body=folder_metadata, fields='id').execute()
return folder.get('id')


# Upload a file to the specified folder
def upload_file(file_path, folder_id):
service = build('drive', 'v3', credentials=creds)
file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}
media = MediaFileUpload(file_path)
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
return file.get('id')


# List files inside the specified folder
def list_files(folder_id):
service = build('drive', 'v3', credentials=creds)
results = service.files().list(q=f"'{folder_id}' in parents and trashed=false", fields="files(name)").execute()
files = results.get('files', [])
for file in files:
print(file.get('name'))


# Usage example
folder_id = create_folder('Test Folder')
file_id = upload_file('sample_file.txt', folder_id)
list_files(folder_id)

This code uses the googleapiclient library to interact with the Google Drive API. The service_account module is used to authenticate your application using the "credentials.json" file. The MediaFileUpload class is used to upload files to Google Drive.

You can run the code to create a folder called “Test Folder”, upload a file called “sample_file.txt” to the folder, and list the files inside the folder.

Conclusion

In this article, we have explored how to use Python to interact with the Google Drive API. We started by setting up the project on the Google Cloud Dashboard and enabling the Google Drive API. Then, we created the necessary credentials and downloaded the JSON key file. We also learned how to create and share folders within our Google Drive account.

Finally, we provided a sample Python code that demonstrates how to create a folder, upload a file to the folder, and list the files inside the folder using the Google Drive API.

By following this guide, you can easily integrate Google Drive functionality into your Python applications and automate file operations, making your application more functional and efficient.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--

Jennie Lee
Jennie Lee

Written by Jennie Lee

Software Testing Blogger, #API Testing

No responses yet