Google Workspace APIs – Using G Suite API
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.
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.
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.