top of page
Writer's picturelakshya saxena

How to Seamlessly Integrate Google Drive API into Your Application


Cover Image

Overview

This article provides a guide on integrating the Google Drive API into your application. By implementing this API, you can seamlessly manage files stored in Google Drive, allowing you to efficiently upload, download, and share them. The integration also enables smooth file management, enhancing the overall functionality of your application.


What is Google Drive API?

The Google Drive API allows you to build applications that utilize Google Drive’s cloud storage. Integrating this API lets you enhance your app with powerful features that interact seamlessly with Google Drive.

How Drive API works

This image explains how an app securely connects to Google Drive to access or manage your files using a security check (OAuth) and specific tools (Drive API).


Important terms :
  1. Drive API: The Drive API is like a set of tools that the app uses to talk to Google Drive. It allows the app to request data from your Google Drive, like files or folders.

  2. OAuth: OAuth is a security system that ensures only authorized apps can access your data. Before the app can access your Google Drive, it needs your permission, managed through OAuth.


Step-by-Step Guide to Integrate Google Drive API with Python

Prerequisites for integrating Drive API:

  1. You must have a Google Account

  2. Python should be installed in your system

  3. Install these via the below command and ensure that you have pip installed in your system

pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib

CREATING A NEW PROJECT ON THE GOOGLE CLOUD CONSOLE

Step 1: Go to Google Console and click 'select a project'.

image showing Select a a project button on console

Step 2: Click on 'NEW PROJECT' and name your project as desired. Then click on 'CREATE'

new project button on console
create new project button on console

Step 3: Once you have created a new project, select that created project and start integrating Drive API.

Steps to select newly created project

You have successfully created a new project on Google Console.


ENABLING GOOGLE DRIVE API

Step 4: Go to 'APIs & Services' > 'Enabled APIs & services'

Enabled APIs & services button on console

Step 5: After that, click on 'ENABLE APIS AND SERVICES'

ENABLE APIS AND SERVICES button on console

STEP 6: Search for 'Drive API' and choose 'Google Drive API'

Google Drive API option on console

STEP 7: Click on ENABLE

Enable Drive API button on console

You have successfully enabled Drive API to integrate it into your application.


SETTING API CREDENTIALS FOR USING DRIVE API

STEP 8: Go to 'APIs & Services' > 'Credentials'

Credentials button on console

STEP 9: Create a Service account by clicking 'CREATE CREDENTIALS' > 'Service account'

Service account button on console

STEP 10: After successfully filling in all details and creating a service account, Select it to add a Key.

select service account link on console

STEP 11: Add a JSON key by clicking 'ADD KEY'

Add key for service account button on console

STEP 12: After creating the JSON key, a file will be downloaded to your system. Please keep it safe, as we will require it to access the Drive API.

choose JSON key for service account

After completing all the above steps, you have successfully configured your settings, and Google Drive API is ready to use and integrate into your respective application.


UPLOADING A FILE

STEP 1: Copy your service account email

Service account email copy on console

STEP 2: Make a folder in your Drive and share that folder with the service account email that you just copied

folder share to service account email on drive

STEP 3: Now, open that folder and copy the folder ID as shown in the image below (this will be PARENT_FOLDER_ID)

Folder id in url of drive

STEP 4: Run the following Python script by setting variables like

SERVICE_ACCOUNT_FILE: File path of JSON key that we've just downloaded

FILE_PATH: Path of the file that you want to upload on your Drive


Python code for file upload :

from googleapiclient.discovery import build
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/drive']

# Variables to be configured by user 
# ======================================================
SERVICE_ACCOUNT_FILE = <SERVICE_ACCOUNT_JSON_KEY> 
FILE_PATH = "<FILE_PATH>"
PARENT_FOLDER_ID = "<PARENT_FOLDER_ID>"
# ======================================================

def authenticate():
    creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    return creds

def upload_file(file_path):
    creds = authenticate()
    service = build('drive', 'v3', credentials=creds)
    file_metadata = {
        'name' : "TestingFile.txt",
        'parents' : [PARENT_FOLDER_ID]
    }

    file = service.files().create(
        body=file_metadata,
        media_body=file_path
    ).execute()

upload_file(FILE_PATH)

Run the above Python script, and you'll see that the file has been uploaded to the Drive.

image showing successfull file upload on drive

CREATING A FOLDER

Python code for creating a folder :

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

SCOPES = ['https://www.googleapis.com/auth/drive']
# Variables to be configured by user 
# ======================================================
SERVICE_ACCOUNT_FILE = <SERVICE_ACCOUNT_JSON_KEY> 
PARENT_FOLDER_ID = "<PARENT_FOLDER_ID>"
# ======================================================

def authenticate():
    creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    return creds

def create_folder():
    creds = authenticate()
    service = build('drive', 'v3', credentials=creds)

    file_metadata = {
        'name' : "TestingMyFolder",
        'parents' : [PARENT_FOLDER_ID],
        'mimeType': 'application/vnd.google-apps.folder' 
    }
    file = service.files().create(
        body=file_metadata,
        fields='id'
    ).execute()

create_folder()

Run the above Python script, and you'll find that a folder has been created on the Drive.

image showing successfull folder creation on drive

Conclusion: Integrating Google Drive API:

Using these steps, one can quickly integrate Google Drive API and can upload files and create folders on Drive using Python without logging into one Google account


  • Uploading a file output video :



  • Creating a folder output video :




FAQs:


Q1. Can I share files and folders via the Google Drive API?

Yes, you can manage sharing permissions by using the permissions. Create a method to grant or adjust access for users or groups.


Q2.  Is there a rate limit for the Google Drive API?

The Google Drive API enforces quota and rate limits to ensure fair usage. You can monitor your quota usage in the Google Cloud Console and apply for higher quotas if necessary.


Q3. How can I handle errors with the Google Drive API?

The API provides standard HTTP status codes and error messages for various issues. To handle errors effectively, check the response codes and messages to offer appropriate feedback or implement retry logic.



References :


Refer to the following links for more detailed information and guides:


Explore the GitHub repository for source code and examples to enhance your projects.


You’re now equipped to master the Google Drive API—code, explore, and elevate your projects to new heights!


25 views0 comments

Recent Posts

See All

How to Use Google Books API

Explore integrating the Google Books API into your application with step-by-step examples, detailed code, and expert tips.

Commenti


bottom of page