[MUSIC PLAYING]
WESLEY CHUN: Hi, this is Wesley Chun, engineer at Google,
and happy to introduce you to the Google Slides API.
It's new.
We've never had an API for slides before.
Isn't that exciting?
With the API, developers can write apps that create
or update slide presentations.
What does that really mean?
Well, there are so many possibilities
I can only give you a few ideas.
An API means you can take that data sitting
in a database or a spreadsheet and make
it more visually appealing.
It also means that you can have a core presentation
with placeholders for text and images
and then customize it in bulk for different customers.
But before I let you noodle out some more great ideas out
of that genius brain of yours, let's
show you how to use the API first
so that when that brilliant scheme washes over you,
you'll know exactly how to make it happen.
In our demo, we'll have just a title slide template,
like what you see here.
There are two placeholders-- one for the presentation name,
and the other, up in the corner there,
is a rectangle where an image will go, like a company logo.
In our example, we're going to use a product icon.
Now, there are other ways of getting images
into a presentation.
This is just one of the techniques.
Both the icon and the template files
are sitting pretty right in my Google Drive folder right now.
If you've guessed that this script uses both the Google
Drive and the Google Slides APIs,
then you're right on target.
But before we get to the code, go
to your project in the Developers Console
to enable both the Drive and Slides APIs,
then download the JSON credentials file.
If you're new to developing, using Google APIs,
be sure to check out our earlier video walking you
through this process.
What's our script going to do?
Copy the slide template, construct the request
to replace those placeholders with a presentation name
and product icon, and then call the Slides API
to execute those requests.
And then you'll end up with your desired new presentation.
Now let's go to the computer and look at the code.
Let's start with lines one through 20.
This block of code includes the standard imports
and boilerplate authorization code that we're probably
already used to seeing.
Now, the things that are different to take
note of in this script include line seven and eight,
which are the file names for the slide template and product icon
that are sitting in our main Drive folder.
Lines 9 through 12 represent the authorization scopes
to be able to copy files in Google Drive,
plus have [? read/write ?] access
to Google Slides presentations.
Now, this is the first episode where
we're using more than one Google API simultaneously.
So we should expect code to look like this-- where you have
multiple scopes in the section.
Now, I choose to have multiple strings-- each one representing
an individual scope.
But you could also put all the scopes
in one giant string, which are space delimited.
To me, this is easier to read.
Then, lines 18 through 20 are where
we're creating the service endpoints
to the APIs using the same [INAUDIBLE] HTTP client.
On lines 22 to 25 is where we use the Google Drive
API to find the template file and copy it,
saving the ID of the new presentation that's created.
On lines 27 to 33 is where we use the Slides
API to find that rectangle placeholder where
the image will go.
Lines 35 to 39 are where we use the Drive
API to find the image file and get the secure URL.
Or it can be downloaded.
That's how the Slides API creates
images and presentations.
All of the real work is in lines 41 to 53,
where we're putting together our set of requests for the Slides
API.
The first is replaceAllText, where the presentation name
replaces the text placeholder that we had in the template.
The second is createImage to replace the image placeholder
with a product icon using that secure URL we just
talked about.
Remember earlier how we grabbed that rectangle
where the image will go?
You know, the placeholder?
Well, we use its dimensions so that the product
icon is re-sized and drops right over that rectangle
placeholder.
Finally, the last request is deleteObject,
because once we have our product icon in place,
we don't need that image placeholder anymore.
And the last lines of code down at the bottom
are where we send all of these requests to the API
and then let the user know we're done.
Let's run this script, which we call slides_template.
Now, you can use Python 2.0 or 3.0.
It doesn't really matter because this script
works with both versions without any additional code.
Assume that we've run our [INAUDIBLE] authorization flow
and accepted the request.
And you can see the output from the print calls
until it's finished.
And now all you need to do is confirm
that the new deck exists in your Drive folder
with all the substitutions.
And if you click on the New Presentation in Google Drive,
you should have something that looks like this.
Now you have an idea of what the Google Slides API can do
in less than 60 lines of code.
If you want a deeper dive in today's code sample,
check out the blog post.
If you want to start with an easier example,
check out the quickstart, which is available in Python
in other languages.
If you're ready to wrap your head around how
the API works in general, check out the first link.
To see the JSON payloads for common operations
you perform with the API, go to the second link.
And to go straight to the API reference docs,
hit up the third link.
The next step is up to you.
It's a green field out there.
The Google Slides API is just waiting for your next big idea.
Go out and build that awesome app.
And be sure to subscribe to our channel,
and tune in again next time.
This is Wesley Chun from Google, and we'll see you soon.
[MUSIC PLAYING]