Hey everybody.
This is Wesley Chun, engineer and developer
advocate at Google.
In this video we'll walk through a simple script that
lists the files on your Google Drive.
But before we do that, why the drive API?
We're not talking Google Apps, syncing files,
or a user Interface integration using the Drive SDK.
No.
The API is about unlocking drive functionality and making
it accessible to your app-- things
like uploading and downloading files, sharing, folders,
metadata, and managing revisions.
With the API, you can write an app that takes a zip file,
decompresses the archive, and uploads those files to a Google
Drive folder.
Or take a stack of photos on Drive, build a collage, or two
and upload the generated image back to Drive.
Wait, here's an even better idea.
All right, how many of you have a massive folder of pictures
from different times and places?
A headache to sort.
Well, what if you could write a little piece of code that
used the file timestamps and the embedded geocoding
metadata in each file image?
Well, is it not past well then to just auto
create photo albums out of thousands of unsorted images?
Well, these are all possible with the API.
The simple script we're building today is just the beginning.
It's your launch point.
The code we'll write is in Python,
but you can use any language that's
supported by the Google API's client library
because the calls are similar regardless of which language
you use.
When using Google APIs, you're either going to need
authorization or you don't.
For example, doing a generic search for YouTube videos
doesn't require authorization, but accessing your Google Drive
and updating your YouTube playlists
do because that's personal data.
And there are code snippets for each case.
So use the right one for the job.
For us, because we're doing Drive today,
you're going to be using the authorize one.
Click the link to check out the earlier episode on this topic
if you missed it.
Since we're looking at code, let's go to the computer
and write that script.
OK.
In a previous episode, we looked at the boilerplate code
that provides authorized data access to Google APIs.
The code is going to be very similar, or exactly the same,
to talk to any API requiring authorized access.
So you should get comfy with it.
Today we're going to use the Google Drive API.
So to start we copy the boilerplate code here
as drivelist.py.
You can call it whatever you like.
In this episode, we want to list out
the files in a drive folder.
It's user data, hence requiring authorized access.
You can't get to this kind of data with just an API key.
Below the imports, the first thing you need to do
is set the scopes variable on line five.
Depending on what you're trying to do,
you may need one or more scopes.
So either you'll have an interval of strings
or one long string with multiple scopes space-delimited.
The only scope needed drive.readonly,
and that's all we have for the scopes variable.
You can get a list of scopes from the API stocks
page or OAuth Playground.
Line six is a client secret file that you
save when you click the Download JSON button on the Projects
Credentials page.
When access is granted to an app,
you're given an access token which
is stored in the file listed on line eight.
Because we'll be running the script for the first time,
we've got no credentials, meaning
that we're going to fail this if statement on line 10.
That initiates the OAuth flow.
Once access is granted, we need to create a service endpoint.
And that's done on line 14.
The API string is drive, since that's
the API we're trying to access, and at the time
of this recording it's on version 2.
So we'll use v2 for the version.
The last parameter is using our credentials
to sign our HTTP request with.
Once everything executes successfully now
we'll have an endpoint to access Google Drive with.
We want to pull down a user's files,
so let's create a files variable.
And we'll use a service endpoint to get access to that.
We'll use the files call to get file access.
Then we'll use the list method to get a list of files.
And then we'll execute that command.
And if all goes well, we'll get a payload
that comes back that has an items field in it.
But just in case, we'll give an empty list so that our for loop
doesn't fail down below.
Now we want to iterate over each file.
For every file in files, what are we going to do?
We're going to display the file's name, which
is given with the title field.
And we'll display its type, which
is given with the MIME type field.
And that is pretty much it.
We actually should be good to go.
So let's give it a shot over in the command line window here.
By the way, if you want to know what other data you
can get access to from the API, just check out the docs.
Now let's run it.
Go through the OAuth flow, get some credentials,
and see some Drive files.
By the way, before I execute this,
at the time of this recording you'll
get a warning saying that the tools.run function is
deprecated, which is true.
There is a newer function called Run Flow,
but it requires additional code on the Python side.
We will look at those changes in another episode.
So for now, let's just use tools.run as is.
It's not quite dead yet.
There's lots of code that still uses it, but at some point
it will be taken away.
So when we run this command, we'll
see our browser window pop up to go through the OAuth 2 flow.
And you can see it come up here.
Now instead of getting the OAuth 2 window,
if you actually get a 401 error saying
you're missing an application name or email just
go to your Projects Consent screen page
and fill out those two fields-- all the others are optional--
and save it.
And then you'll be able to get to this exact screen here.
Once a user accepts, a valid access token
is sent back and saved in the storage.JSON
file on lines 8 and 12.
And then it'll just execute our code down here.
So we should see a bunch of Drive files pop up here.
And boom, there they are.
All right.
That was less than 20 lines of code accessing your Drive
files, and it's just the beginning.
Thinking about a mobile app?
Well, Drive has both Android and iOS APIs.
Check out the developer docs to find out more.
Well, we hope we've inspired you in this episode
to see what you can do with Google Drive.
This is Wesley Chun from Google, and we'll see you here
next time on The Launch Pad.