WESLEY CHUN: Hi.
This is Wesley Chun.
You know how uploading and downloading files is pretty
easy for Google Drive users?
Well, today, we're going to show you
that it's just as easy for developers using the Drive API.
As a bonus, we'll also introduce the latest and greatest version
of the API, so let's start there.
The Drive API was launched along with Google Drive back in 2012.
Since then, the team has been innovating and improving
the API along with listening to user feedback.
In the newest version, v3, we changed to more sensible method
and field names, removed duplicate functionality,
and reduced the amount of data returned from API calls.
As a result, v3 is leaner and faster.
I caught up with Steve Bazyl, who's the developer platform
engineer for Google Drive, to help me introduce developers
to v3.
So I gave a quick intro to the new version of the API
at the top of the show, but maybe you
could tell us what was the team specifically trying
to accomplish with this release?
STEVE BAZYL: Sure.
So when we launched the API a couple years ago,
we did what we thought was right.
But after a few years of seeing how
the API was used in production and talking to developers
about what they were trying to accomplish,
we found that there's a really good opportunity
to simplify the API and focus on speed and performance.
WESLEY CHUN: So in addition to the space and time
savings, were some things made easier for developers?
STEVE BAZYL: Definitely, yes.
I think one of the biggest improvements
was around downloading files.
In the older version of the API, downloading and exporting
Google Docs and Sheets was a little bit complicated,
and having to deal with export links.
So we created a new API method explicitly
for exporting documentation, and downloads in general
are a lot simpler in the API.
WESLEY CHUN: So one of the things
that developers are concerned with when a new API comes out
is that the old one or the current one
is immediately obsolete.
Is that the case with this one?
STEVE BAZYL: Not this time.
Normally that would be the case when we release a new API,
but for now, we're going to keep both versions of the API
running.
V3 doesn't really add any new features.
It's primarily focused on simplifying the API,
making it faster out of the box, and it's really
targeted for new developers getting started with the API.
So for those who have existing projects, no need to panic.
V2 is going to continue to work for really
the next couple years.
WESLEY CHUN: OK.
Well, cool.
So let's say I do have an existing
app using the current version, which is v2.
Is it possible for me to start a new component
to that app using v3?
Can they kind of live side by side?
STEVE BAZYL: Of course, and that's a really great way
to get started with v3.
Just pick something that is new, maybe some code
that you want to refactor.
That's a great opportunity to dip your toes into v3,
understand what changed, how it would benefit you.
And if that goes well, those people
might find out that the rest of their application
would benefit from moving to v3.
But there's no rush to do it.
People feel like they have the time
and they think they would benefit, go for it.
There's a lot of gains that they'll get.
But again, no need to panic.
Really just take it easy.
WESLEY CHUN: OK.
Cool.
So there's a lot of developers out there using Google Drive,
but they may all be using different development
environments.
What languages are supported by the Drive API?
STEVE BAZYL: So we have client libraries
for most of the common languages out there.
So if you're java.net, you do Objective-C or Swift for iOS,
PHP, Python--
WESLEY CHUN: Python.
Oh, that's my favorite.
STEVE BAZYL: Python 2.
There's client libraries for most languages, Node.js, even
a client-side JavaScript library,
and they all work with the API.
Some of them are easier to use than others,
but really, all of them are going
to get you a lot closer to having
your code ready than if you just wrote your own client.
And of course, these are REST APIs.
So if you are more comfortable dealing
with just whatever HttpClient you
have or some other REST library that you
happen to be using, go ahead and use those.
It's going to work just the same.
You might have to do a little bit more work, particularly
for uploads and downloads and some
of the more complicated parts, but the vast majority of it
is pretty easy to use pretty much on any platform.
WESLEY CHUN: OK, great.
So that's good news that a lot of developers out there
could use the Drive API.
STEVE BAZYL: Exactly, yeah.
WESLEY CHUN: All right.
Well, Steve, that was really useful information.
I think developers have all they need to know.
All we need to do is get them started with it.
Thanks for stopping by today.
STEVE BAZYL: OK.
Thanks for having me.
WESLEY CHUN: Now let's help you start using v3.
We're going to walk through a simple code snippet
that uploads and downloads files with the API,
including converting to Google Apps format
and exporting as PDF.
We'll do this first using the current version, v2, then
show you how we improved things for v3.
So put on your coding hat, and let's go to the computer now.
As in previous episodes, we're going
to do it in Python for brevity, and it
makes for great pseudocode if you're
using something else as your dev tool.
To see all the Google API's client library
supported languages, click the link onscreen
or down in the video description below if you're on mobile.
Now, the first 20 or so lines are
nearly identical to the auth boilerplate
we covered in depth at the beginning of the video series.
Check out those videos, especially the one
on the common code walkthrough if you
missed these to understand what it does and why it's needed.
There are some differences in this modernized version.
The one you should note are there's
an import of the print function on line 1.
That will allow your script to run in Python 2 or 3.
Then there's this import block for the argparse module
in lines 20 and 21 so that your script uses the new run flow
function from oauth2client.tools, which
succeeds the run function but falls back to the latter
if argparse is not available, meaning you're using an older
version of Python.
Visit the link to the blog post to find out
more info about this change.
Since we're using the Drive API, we need the correct scope,
and that's on line 14.
Use the most restrictive scope you can.
Here it's drive.file because we're
creating files on Google Drive.
The more restrictive your scope, the fewer permissions
you need to ask of your users, and the safer
your code will be.
Now the first step.
Create a service endpoint to Google Drive,
as you can see on line 22.
As mentioned earlier, we're going to do this in v2 first,
then show you how the code changes for v3.
Lines 24 to 27 are the files to upload.
You can see that they're two tuples,
with file names and a flag for converting to Google Apps
format, a yes or a no, and you can also
see that we're uploading the same file twice--
first as is in plain text, then convert to Google Apps format.
For the plain text conversion, it's Google Docs.
But you can imagine that for CSV files,
it'll be Google Sheets, right?
Now to the main part of the script.
We're going to start with the section to do uploads.
The for loop calls the files.insert command
for each upload with parameters.
The first is the convert flag, the body
is the file metadata, which is just the file name.
The media body is also the file name,
but it's slightly different.
This is where the data is going to come from,
meaning that this file is going to be opened
and the content sent to the API.
Finally, you should pass in the fields you want returned.
Why is this important?
If not specified, you get back a payload
with more than 30 fields.
Why waste all that network traffic when you only
need a couple of them?
I just want the MIMETYPE to confirm
how the file was stored in Drive,
and I also want the export links.
More on that soon.
On lines 33 and 34, if the call is successful,
we're going to display the file was
uploaded to the user, including the MIMETYPE.
The final section of the script is
where we wrap up our poor man's plain text PDF conversion.
That is, to export and download the Google Docs
version of the file as PDF.
Note that we still have the result
from the second upload, which is the Google Docs converted file.
Back to the export links.
We requested that field because it
has the download links that we need, specifically the PDF one.
How do we use it?
Well, we need to issue an authorized HTTP get request
because there's no API call for this, at least not in v2.
After the call completes, check if we got the data.
If so, save to disk and display.
Success.
OK.
Let's save it and run it.
If you didn't already know, the first time
you run a command-line script like this, a browser window
is going to pop open with the OAuth2 prompt, where
you have to authorize this script to be able to get access
to the files in your Google Drive,
or your users' Google Drive.
When you opt in, the credentials are
going to be downloaded and stored in this storage.json
file, so you only need to do it once
unless you delete this file.
Anyway, let's just pretend that all that has happened already,
and so we're just going to run the script as if we didn't have
to worry about authorization.
We should see output for the plain text version-- there
it is-- and in the Google Docs converted version, and finally,
the download into PDF.
Now let's check the Drive UI, web or mobile--
since I'm on a computer, obviously it's
the web version-- to confirm that we've
got two versions of the Hello file uploaded.
So here's the plain text one.
You can get a preview of it.
There it is.
And then you can also get a preview of the Google Doc
converted one, and there that is.
The last thing we need to do is check that we have a PDF file,
and we can do that pretty easily from the command line,
confirm that it actually is a PDF file, and then open it.
And there we are-- helloworld.pdf.
Great.
Now let's do the same thing, but use v3 of the API instead.
So first, change the API version to v3.
Now, one key change.
There's not going to be a convert flag anymore.
The problem is that a Boolean variable only gives you
two choices-- Google Apps conversion or not at all.
There's no option to specify what format you want.
If MIMETYPEs was used instead of Booleans,
there's more flexibility for conversion, either uploads
or downloads.
So in the first case, let's change the false to none, which
is Python's null object, meaning no conversion.
And in the second case, we need the MIMETYPE for Google Docs,
and that's easily found.
Now we need to change this convert variable
to reflect this change of data.
Now, we mentioned that v3 has better and more sensible
naming.
Here, the title field changes to name, more accurately
reflecting that a file's name is not its title,
but hello, its name.
Another change with using the MIMETYPE instead
of the convert flag is that instead of being a parameter,
it's actually going to be part of the file metadata,
so we need to add that if doing conversion.
Another improved name change in v3
is that instead of insert, create is actually
a better name or verb.
So that's what we're going to change this to.
We have to get rid of the convert flag,
the body stays the same, the media body stays the same.
Lastly, fields is no longer required, as v3 is more nimble,
returning much less data than v2.
Fortunately, the things that are returned
are actually the ones we need.
Finally, if successful, display the uploaded file message
to the user.
Now let's see how export and downloads are simplified.
First, another v3 change.
No more download links.
In other words, we don't need to look for export links
and make an HTTP call.
Instead, both steps have been simplified to a single API
request to the files.export method.
If successful, the call will yield the data we need to save.
So write it to disk and output to the user just as before,
and that's it.
Now let's save and run the v3 version.
The output should be exactly the same as v2.
There's the plain text, there's the Google Doc conversion,
and a download to PDF.
Now double check in your drive UI that you got another two
files, and there they are.
I'll let you confirm that the PDF was downloaded again
successfully.
And that's it.
Now you know how to upload files to Google Drive using
v2 and v3 with or without conversion to Google Apps
format.
Awesome.
So as you can see, it's not too bad if you're using v2.
But better yet, if you use the newest version,
you can take advantage of all the improvements that we made.
Either way, we hope this sample inspires
you to write your own app using the Drive API.
To get started, click the link that
takes you right to our docs.
Want a deeper look at the code covered in this video?
Well, jump to the blog post at the second link.
And to see other DevBytes in our developer video series,
check out the links at the bottom.
Now that you know how to upload and download files
with and without conversion, we hope
this jumpstarts your next project using the Google Drive
API.
If you like this video, subscribe and check out
our other DevBytes.
I'm Wesley Chun from Google, and we'll
see you the next time on the Launchpad Online.
[MUSIC PLAYING]