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 SCOPES = 'http://www.googleapis.com/auth/drive/drive.metadata.readonly' 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 SCOPES = 'http://www.googleapis.com/auth/drive/drive.metadata.readonly' #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: 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
Google Workspace vs Microsoft 365: Features, Pricing, and Collaboration Compared In today's digital landscape, businesses and individuals rely heavily on cloud-based productivity suites to manage their work, collaborate with teams, and store data securely. Google Workspace (formerly G Suite) and Microsoft 365 are the two major players in this space, offering a suite of applicati
9 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
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