Open In App
Related Articles

Google Workspace APIs – Using G Suite API

Like Article
Save Article
Report issue

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:

  1. Getting the credentials for G Suite APIs
  2. 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.


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


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
#store & refresh tokens
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:
    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 ='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 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.

Last Updated : 24 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads