WESLEY CHUN: Hey, welcome back to our series on using G Suite
developer tools for student developers.
In this segment, we'll look at using G Suite REST APIs along
with code samples.
I'm your host, Wesley Chun.
If you missed the overview video,
check it out first because there we show you how to get
started, getting credentials and creating projects
using G Suite APIs.
Now we're going to dive in, starting with Google Drive.
Here's code that lists the first 100 files and folders
in your Google Drive.
Believe it or not, the real application
is really just the last three lines.
Everything else is just the security boilerplate
we covered earlier plus a requested scopes and 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.
If you want to build this app line by line
check out the Code Map.
Heads up, 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, including
the deep dive video linked here.
While this sample is in Python, you
can guess that the client library
upgrades like this apply regardless of what language
you use.
OK, now that you've seen some code let's
take a step back and think why you would use the Drive API?
Well, one reason could be, you want
to write an app that backs up zip files
but expands them in flight to Google Drive.
Well, this is an actual JavaScript sample app.
Check out its open source repo if interested.
Another example, let's say you get a job at a startup helping
people automate photo albums.
Well, you'd pitch that everyone who gets back from vacation
have to empty out their cameras and phones
to an external hard drive.
And what do all those pictures look like on disk?
A big unorganized mess, right?
But 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.
Get an idea of what you can do with Google APIs now?
It's as limitless as your imagination.
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 guides tab that
feature 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 any open source repos or developer videos.
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.
Next, let's move to the Sheets API.
After all, aren't spreadsheets the most programmable
of all the productivity apps?
You may have data sitting in a database or a CSV file,
but those form factors aren't exactly
visually appealing, right?
Do you really want to tell your boss to select star
from your database?
Well, spreadsheets are a common and simple user interface
that feature tabular data.
And JavaScript developers can try the API code
lab that lets you build a customized reporting
tool for a fictitious toy manufacturer.
Now in this code sample, we're going
to reuse that toy order database and migrate it
to a spreadsheet.
Again, the OAuth code will be the same as before,
but switched with the Sheet's rewrite scope and its API
endpoint.
Now as you can see, the first half of the code snippet
queries for the toy order data while the second half
creates a new sheet and pushes the data there.
Check out the video for more on this sample.
Don't forget to enable the API first!
Moving on, why the Slides API?
Well the same reason as Sheets but taking it a step further.
Yes, Sheets is definitely more visual than a database
but isn't a slide deck the ultimate in visualization?
Or maybe you're organizing your school's hackathon
and want to create only one master slide deck when
seeking corporate sponsorships.
Or you're a sales person who's giving the same pitch
to multiple clients.
Well, why not create one master template deck,
then auto generate customized decks
for each sponsor or customer?
Now here's that code snippet, replacing the placeholder
variables with actual data, whether it's the customer
name or corporate logo.
Now this one's also got a video for further study.
In real life you can probably guess
this isn't the whole thing.
And you will need to use more than one API.
Well, think about it.
Don't you have to call the Drive API
to copy the master template, then get the customer
or sponsor information from the Sheets API,
then generate the slide deck with the Slides API?
Well, this pseudocode answers the obvious question
of how to set up using more than one API at a time.
Now here's another example using two APIs.
Here, we take data and a chart from a spreadsheet
and add them to a slide deck.
The video also shows how to read the data from the Sheets API
first.
Now the Docs API is a perfect tool
for creating a mail merge tool.
Similar to the customer Slide decks,
let's say you got a form letter with variable placeholders
like this.
Then you have a sheet with the data for each recipient.
Well, here's the heart of the mail merge code.
Look how similar it is to the Slides example.
Once you copy the master template doc
and read the data from the sheet,
it's not much effort to fill in those variable placeholders.
Now this video doesn't go over the code
but you can get a link to the app's open source repo.
Once it's done executing you should
have an individual Google Doc with all the substitutions
made.
All you need to do is call the Drive API to export it as PDF,
then build an email message with this attachment,
and then send it with the Gmail API.
All right, so that's our quick tour of G Suite REST APIs.
To learn more about others, check out our landing page.
In the next episode, we'll cover a higher level platform that
lets you access G Suite APIs with just objects
and not have to worry as much about authorization code.
Join us as we tell you about Google Apps Script.
This is Wesley Chun from Google, and we'll see you next time.