[MUSIC PLAYING]
MALE SPEAKER: Shanghai GDG is a very
interesting developer community.
FEMALE SPEAKER: I'm glad somebody
has asked this question.
MALE SPEAKER: This is where the magic happens.
FEMALE SPEAKER: This is primarily a question and
answer show, so if any of you out there
would like to ask questions.
VIC FRYZEL: Hey guys, welcome back to Google Developer Live.
I'm Vic.
I work on the Google Drive SDK team.
Today, what I'm going to be doing is walking through the
Quickstart sample for the Drive SDK and Ruby.
So today I'm going to show you how to write a command line
application that will upload a file to Google Drive.
So this is pretty straightforward.
The entire tutorial takes five minutes or less to really run
through yourself.
There's just a few key steps that we have to do.
So here on my screen, I've got the Google Drive SDK
documentation open, specifically
our Quickstart guide.
And there's a lot of text here, but the primary points
are that you need to enable the Drive API for a Google API
Console project.
That pretty much lets your application just
access the Drive API.
You need to install the Google Ruby client library, set up
the sample, and run the sample.
So let's just dive in.
The first thing is open the Google API Console, and then
create a new project.
So I'm going to create one here.
I'm going to call this Google Developers
Live, and Create Project.
This just takes a second to create the new project.
And here it is.
So you'll notice that most of the services are off.
What we have to do is enable the Drive API.
Click this.
And then, that's it for that section.
The other thing that we have to do in the API Console is
create an OAuth 2.0 client ID, and what this is going to do
is allow our command line application to actually make
requests to the API that have been authorized by a user.
So I'm going to create the client ID.
Again, I'm going to call it Google Developers Live.
And then I'm going to click Next.
So this is important.
We're not writing a web application.
We're writing an installed application, so this is an
application that doesn't run in a web browser.
So let's select this.
And then it's not an Android or iOS application, it's
Other, and then I'm going to create the client ID.
All right, so here it is.
This is our client ID, and this is our client secret.
We're going to need these in just a minute.
Coming back to the documentation, the next thing
that we have to do is install the Google API
client library for Ruby.
So this is pretty much just a Ruby gem.
I'm going to come here and paste it.
I've actually already got it installed, and I need
superuser permissions to install it and stuff, so I'm
not going to run that command, but just trust me
that I've run it.
And once that's installed, we need to set up the sample.
So, under Ruby here, the only thing that we need to do is
copy and paste this source code into quickstart.rb, which
is the name of the file that we're going to run.
So I've got that here locally.
You can see, this is the exact same.
And then the next thing that we have to do is replace these
templates with an actual client ID in secret.
So these are in the API Console for the installed
application, so let me select this one, and there's the
client ID, and then we replace this one
with the client secret.
And that's it.
So I'm going to save it, and then we'll run it, and after
we run it once, we're going to walk through the source code.
So to run it-- this is also in the documentation--
we just come down a little bit further, and you can see all
you have to do is run Ruby quickstart.rb.
So paste to that--
oh, oops.
Before I forget, let me also go into the fact that we're
going to upload a file, so we need a file to upload.
I actually have one here called document.txt.
It says "Hello world" inside.
Nothing fancy.
So again, let me run the quickstart.
So you see some warnings appear.
Just ignore those.
Those are because my Ruby installation is a
little messed up.
But no big deal.
It's still functioning fine.
So here, what we need to do is paste this URL
into our web browser.
So I come here, and I paste.
So here we see the OAuth 2.0 authorization page for our API
project, Google Developers Live, and it's asking me to
view and manage the files and documents in Google Drive.
If a user's curious, they can expand this and see what else
is going on.
So I'm going to click Allow Access, and because this is
for an installed application, what it's going to do is give
me an authorization code that I have to take from this text
box and paste back to the application.
So once I paste that here and hit Enter, that's it.
So here, we have the document ID for our file
and then the title.
These were returned from the Drive SDK back to the Ruby
client library, and then we printed them out in our
application.
So let's take a look at Google Drive, and I'll show you that
that actually worked.
So here, I'm searching for my document, which is the name of
the document we uploaded, and there's nothing.
But if I refresh, here it is.
So I click on this and give it a second.
It says "Hello world." So that's the content of our text
file, so we know that it works.
Let's just now take a look through the source code and
see what's going on.
So if I--
then quickstart.rb.
So at the top here, we're just importing some libraries, and
then setting some constants here primarily the client ID
in secret, and then the OAuth scope that
we're going to request.
Here, we requested the full drive scope, so this allows us
to pretty much access any part of the Drive API.
There are other scopes that are probably more appropriate,
but because hypothetically this application might in the
future download files or something like that, or work
with a user's entire drive corpus, we
request the full scope.
And then also we need the redirect URI, which is
currently set to out of band, which is why we had to copy
and paste that authorization code.
Then we create a new API client and load the drive API.
Pretty straightforward.
We request authorization, so we set up some parameters
here, and then we get the URI from client.authoriza
tion.authorizationuri.
We print that to the screen.
This is the thing that the user will copy and paste into
their web browser, and once they paste it, we're going to
request that they enter the authorization code, and
client.authorization.code equals what they gave, and
then we're going to fetch the access token.
Pretty straightforward.
The last thing that we have to do is actually
just insert the file.
So here, we're creating the new file with this metadata.
So here's the title and the description and the MIME type.
And then, here's the media content that we're going to
attach to that metadata.
So document.txt, and we're saying that its
type is text plain.
And here's the result.
Here is where we actually upload this
file to Google Drive.
We're just doing client.execute, and we're
doing drive.files.insert, and we're attaching some extra
parameters here.
Once we do that, result contains a JSON object or a
JSON representation of the API response, and result.data dot
whatever is going to have metadata about the individual
file that we uploaded.
So result.data.id is the ID of the file, and title is title
of the file.
There's other stuff in here that's not too important at
the moment.
So that's pretty much it for the application.
So once we've got that done, we can actually improve the
sample quite a bit.
We could do some other things, primarily this stuff, like
copying and pasting this URL into the browser, then
clicking Allow Access, then copying and pasting the
authorization code back into the application.
That's just tedious for the user, so there are a couple of
ways around that.
What we want to do usually is open the web browser for the
user, firstly, so that they land on the authorization page
without having to copy and paste the URL.
The other thing we usually want to do is spin up a web
server locally on the user's computer just for a few
minutes while the user browses to the authorization URL.
And then what we can do is set a redirect URI so that when
Google receives the Allow Access click from the user,
Google will redirect the user's browser
back to local host.
And once they do that, you can land here on your own web
browser and get the URI of the request which has the
authorization code in it.
And with that authorization code, you can use that to then
upgrade to a request and an access token.
So that's a much better user experience where the user
doesn't have to copy and paste some stuff that they might not
necessarily understand.
Some other stuff that you could do is make this
application work for more file types besides text files.
So at the moment, if we come back to quickstart.rb, you can
see here that we manually set the MIME type, but you could
do some more intelligent determination of what the MIME
type of the file is before uploading it.
Another thing we can do is extend just the general
functionality of this command line sample.
We could do things like allow the user to download a file
with the command line sample, or we do things like cache the
refresh and access tokens into a secure data store locally on
the user's computer.
So in Linux, you might use a keystore, for instance.
So those are just a couple of things that we could do.
If you're really looking more towards writing a web app that
integrates with the Google Drive user interface, then I
highly recommend you take a look at our other samples,
which are back here in the documentation
under Example Apps.
So here, we have a full set of example apps.
So here's the Ruby one for writing an example application
called DrEdit, which is a text editor for the web.
And that's very much more advanced in that it allows you
to integrate your application into the Google Drive user
interface so that a user can interact with individual files
using your application, and it does a few more advanced
things that just take a little bit longer to get started.
Certainly more than five minutes.
So that would be a good next step.
That's pretty much it, though.
So this has been the Google Drive SDK Quickstart for Ruby.
I'm Vic.
Thanks for watching.
[MUSIC PLAYING]