Google Workspace APIs – Using G Suite API
Last Updated :
24 Jan, 2021
Most of us are familiar with various Google Workspace products(also called G Suite) like Calendar, Drive, and Gmail, etc. But along with these products, Google also provides APIs to access the G Suite products to build your own service as you can customize them according to your needs.
In this article, we’ll look at using G Suite REST APIs along with code samples. For this, you need to know the below two things:
- Getting the credentials for G Suite APIs
- Creating a project using G Suite APIs
We will be starting with Google Drive. Here’s the code that lists the first 100 files and folders in your Google Drive.
Python3
fro __future__ import print_fnction
from googleclient import discovery
from httplib2 import Http
from oauth2client import file , client, tools
store = file .Storage( 'storage.json' )
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets( 'client_secrets.json' )
creds = tools.run_flow(flow, store)
DRIVE = discovery.build( 'drive' , 'v3' , http = creds.authorize(Http()))
files = Drive.files(). list ().execute().get( 'files' , [])
for f in files:
print (f[ 'name' ], f[ 'mimeType' ])
|
The real application is really just the last three lines. Everything else is just the security boilerplate and a requested scopes and an API endpoint creation. Once you have an endpoint to the Drive API, it’s as simple as creating a file listing query, executing that query, and displaying the results.
Google’s upgrading to newer client libraries to bring the GCP and G Suite worlds closer together. While the current libraries will stay operational for a while, you need to see how the code changes. So here’s the exact same example but with the newer libraries.
Python3
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.auth_oauthlib.flow import InstalledAppFlow
from googleapiclient import discovery
creds = None
TOKENS = 'token.p'
if os.path.exists(TOKENS):
with open (TOKENS, 'rb' ) as token:
creds = pickle.load(token)
if not (creds and creds.valid):
if creds and creds.expired and creds.refres_token:
creds.refresh(Request())
else :
flow = InstalledAppFlow.from_client_secrets_file( 'client_secret.json' , SCOPES)
creds = flow.run_local_server()
with open (TOKENS, 'wb' ) as token:
pickle.dump(creds, token)
DRIVE = discovery.build( 'drive' , 'v3' , http = creds.authorize(Http()))
files = Drive.files(). list ().execute().get( 'files' , [])
for f in files:
print (f[ 'name' ], f[ 'mimeType' ])
|
As you can see, aside from a couple of import changes, managing the OAuth tokens yourself, everything else stays mostly the same. The G Suite docs already reflect this change but bear in mind that most of the code in the wild still uses the original libraries. While this sample is in Python, you can guess that the client library upgrades like this, apply regardless of what language you use.
One reason to use the Drive API could be, you want to write an app that backs up zip files but expands them in flight to Google Drive. Another example, let’s say you get a job at a startup helping people automate photo albums. And, you’d pitch that everyone who gets back from vacation has to empty out their cameras and phones to an external hard drive. If you use the Drive API to access file metadata like the timestamp and geolocation, you and your team could actually build an app that auto generates photo albums. Now taking it a step further, stitch those photos together into a video, upload it with the YouTube API, then use the Gmail API to tell your friends and family.
Each Google API has official documentation. And the G Suite ones live at developers.google.com/ whatever the API name is, such as Drive. The docs are structured via the guides tab that features quick starts in multiple languages as well as guides for specific API features.

Now beyond the quick starts are more fully fleshed out sample apps. These live under the samples tab for each API and links to the open-source repositories or developer videos are also available here.

Now if you need help, the Support tab links to stack overflow, or issue tracker for bugs and feature requests, or perhaps communities of like-minded developers.

Similar Reads
Basics of API Testing Using Postman
APIs(Application Programming Interfaces) are very commonly used in development. Postman is a tool that can be used for API Testing. In this article, we will learn how to do simple API Testing using Postman. Go to your workspace in Postman.Click on the + symbol to open a new tab.Enter the API Endpoin
2 min read
Google Cloud Platform - Overview of G Suite APIs
Many of you know how to use Gmail, Google Drive, and Docs, and that's great. But can you code them too? In this article we aim to learn about G Suite as another tool, giving you the ability to code those apps you know so well. Productivity tools like a word processor, spreadsheets, and presentation
5 min read
Google Cloud Platform - Setting Up a Game Server
In this article, we will set up a server in a public cloud to serve a game application by using infrastructure as a service (IaaS) provided by the google cloud platform. However, the process will be nearly the same for any other public cloud platforms. Overview of steps:Set up the account and create
4 min read
Getting Started with Google Actions
Actions on Google is the developer platform for the Google Assistant. By creating actions, brands are able to bring their services to the Google Assistant. Some important terms related to Actions on Google: Actions An interaction with the to start a conversation, the user needs to invoke your Action
4 min read
Google Cloud Platform - Ways of Serving files from GCS
When it comes to the cloud, there is more than one way to serve a file. In, this article, we will walk through all the different ways to serve a file from Google Cloud Storage. It is kind of like having a yard sale, but all the stuff stays in your yard or you decide where it goes. You probably alrea
3 min read
Google Cloud Platform - Ways of Uploading Data to GCS
Before you can harness the power of the cloud, to serve your content, you have to get your data into it. In this article, we will look into all the different ways you can upload data. We all know that Google Cloud Storage (GCS) can serve your binary assets to users worldwide at high speed and low pr
3 min read
A Beginner's Guide to 30 Days of Google Cloud Program
Google Cloud in collaboration with Developer Students Club (DSC) provides an opportunity to students to start their journey in cloud programming with hands-on labs on Google Cloud Platform that powers apps like Google Search, Gmail, and YouTube. Along the way, you will learn and practice cloud conce
4 min read
Robotic Process Automation(RPA) - Google Form Automation using UIPath
In this article, we are going to learn how we can automate filling the Google Forms using Uipath Studio. This project is a basic application of Robotic Process Automation (RPA). The user just needs to provide the Excel file that holds the data to be entered in the form and that's it, rest of the wor
4 min read
How To Change The Project In GCP Using CLI Commands ?
Google Cloud Platform(GCP) offers various Cloud services such as cloud storage, cloud computing, deploying and scaling applications in the cloud e,c. GCP offers pay-per-use services. Apart from these, GCP offers many other services like hosting your websites in the cloud, running your own Operation
4 min read
Google Cloud Platform - Cloud Storage
Google Cloud Storage is unified object storage. In reality, the GCS is the place where you can store and serve static binary assets either for your app to use or directly to your users. But as straightforward, as it sounds, there is a lot going under the hood. The GCP has Buckets, Objects, and vario
2 min read