Integrating Google Drive API with Python: A Step-by-Step Guide

Kanish Chhabra
The Team of Future Learning
3 min readDec 6, 2023

Introduction

Cloud storage solutions like Google Drive offer robust APIs that allow developers to interact with files and folders programmatically. This guide provides a step-by-step process to seamlessly integrate Google Drive API into your Python project.

Prerequisites

Before getting started, ensure you have the following:

  • Google Account: Sign in to your Google Account or create a new one.
  • Google Cloud Console: Access to Google Cloud Console to create a project and enable the Drive API.
  • Python and Libraries: Ensure Python is installed. Install the necessary libraries:
  • google-api-python-client
  • google-auth-httplib2
  • google-auth-oauthlib
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib

Steps to Integrate Google Drive API:

1. Creating a Project in Google Cloud Console

  • Sign in: Go to Google Cloud Console and sign in using your Google Account.
  • Create a New Project: Click on “Select a project” at the top and then “New Project”. Give it a name and create.

2. Enable Google Drive API

  • Navigate to APIs & Services: In the left sidebar, go to “APIs & Services” > “Dashboard”.
  • Enable APIs: Click on “Enable APIs and Services”. Search for “Google Drive API” and enable it for your project.

3. Create Credentials

  • Generate Credentials: In the left sidebar, go to “APIs & Services” > “Credentials”.
  • Create Credentials: Click “Create Credentials” > “Service Account”. Fill in details, choose a role, and create a JSON key. Save this JSON key securely.

4. Authenticate Your Application

  • Download and Use JSON Key: Store the downloaded JSON key securely on your local machine. This key will be used for authenticating your application to access the Drive API.

5. Setup Python Environment

  • Install Required Python Libraries: Open your terminal and install the necessary Python libraries using pip:
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib

6. Uploading a File to Google Drive


from googleapiclient.discovery import build
from google.oauth2 import service_account

creds = service_account.Credentials.from_service_account_file(
'path/to/your/json/key.json',
scopes=['https://www.googleapis.com/auth/drive']
)

drive_service = build('drive', 'v3', credentials=creds)

file_metadata = {
'name': 'MyFile.txt',
'parents': ['<folder_id>'] # ID of the folder where you want to upload
}
file_path = 'path/to/your/local/file.txt'

media = MediaFileUpload(file_path, mimetype='text/plain')

file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()

7. Creating a Folder in Google Drive:


folder_metadata = {
'name': 'MyFolder',
'parents': ['<parent_folder_id>'] # ID of the parent folder (optional)
}

folder = drive_service.files().create(body=folder_metadata, fields='id').execute()

8. Downloading a File from Google Drive:


file_id = 'file_id_to_download'
file_path = 'path/to/save/downloaded/file.txt'

request = drive_service.files().get_media(fileId=file_id)
fh = io.FileIO(file_path, mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False

while not done:
status, done = downloader.next_chunk()

Conclusion

This guide demonstrates how to seamlessly integrate Google Drive API into your Python project, enabling various functionalities such as uploading files, creating folders, and downloading files. By following these steps and using the provided code snippets, you can efficiently interact with Google Drive programmatically.

Thanks for reading!…

If you thought this was interesting, leave a clap or two, and subscribe for future updates!!!

--

--