[MUSIC PLAYING]
MALE SPEAKER 1: Shanghai GDG is a very interesting
developer community.
FEMALE SPEAKER 1: I'm glad somebody
has asked this question.
MALE SPEAKER 2: This is where the magic happens.
FEMALE SPEAKER 1: This is primarily a question and
answer show so if any of you out there
would like to ask questions.
VIC FRYZEL: Hello, everyone.
Welcome back to Google Developers Live.
I'm Vic Fryzel, and this is Nicolas Garnier.
NICOLAS GARNIER: Hello.
VIC FRYZEL: Today we're going to be talking about the Google
Drive SDK again, specifically around how to get started
using Java and the Drive SDK.
So what we're going to do today is walk through the Java
Quickstart example that we provide in the Drive SDK
documentation.
And we're going to show that on the
screen here in a minute.
Essentially, what the example does is
upload a file to Drive.
It's a basic command-line example that you can run by
downloading the Java client library, setting your client
ID in secret, and then just running it.
NICOLAS GARNIER: Yep, that's right.
VIC FRYZEL: So we're going to walk through how to do that.
So first, let's take a look at the documentation.
Can you pull this up here?
So here's the Drive SDK homepage, but we need to pull
up the Java Quickstart example.
So we're going to click Get Started Now, and then this is
going to take us to the Quickstart page.
We're having a bit of network latency at the moment.
OK, so here we go.
This is going to walk you through in about five minutes
how to set up the Java Quickstart example.
So you can see here that Java's highlighted already.
But all of the different languages have to walk through
these first steps.
So do you just want to talk about these?
NICOLAS GARNIER: Yeah, I'll do the first steps, Vic.
So the very first step is to create an API project in the
Google API console.
I'll show that in a minute.
Then basically enable the Drive API and create your
OAuth 2 credentials.
That will allow you to basically tell us who you are
as a developer, register your application, and where we'll
give you a quota, and you'll be able to use OAuth 2
basically in your application.
So the first step you need to do here is the Google APIs
console, which you can access through the URL code
google.com/api/console.
You need to create a new project.
So you go into the little menu on the top
here and click Create.
If you have no project, you'll have one big button in the
center of the screen.
It will say Create a Project.
You can give it any name.
I'm just going to leave that name.
The project is getting created.
Here you go.
And the second step is to enable the drive API because
the API we're going to use with the OAuth 2 credentials
that we are going to create.
If you want to use any other APIs in your code later on,
just come here, enable the other APIs, and your OAuth 2
credentials will work for these APIs as well.
So the first step is to go to API access here and create an
OAuth 2 client ID.
So you have to give another, again, your product name.
We're going to go with--
I don't know--
I like testproject.
Then, for this particular sample, you need to select
Install application because it's a command-line tool, so
it's not a web application.
It basically means that the application doesn't really
have access directly to a browser and cannot do the
OAuth 2 full server-side flow where the application directs
you to a website and you get redirected back.
So you have to choose Install Application here.
OK, create a client ID, and that's it.
We got our client ID in secret.
So let's go back to the steps you want to follow up.
VIC FRYZEL: Yeah, so the next thing Nicolas is going to do
is download the Google Drive Java client library.
So this is essentially a Java library that allows you to
easily interact with Google APIs.
So this is something we provide as
an open source project.
I think Nicolas already has it downloaded here.
NICOLAS GARNIER: Yeah, it's about, I think, 11 megabytes.
So just to avoid waiting for the download, I have the zip
file here, which I just unzipped.
VIC FRYZEL: Yep.
So it contains just a bunch of jars.
And what we're going to do is add these jars to our class
path and then download the actual source code that we're
going to run, which is just a short little hop here.
NICOLAS GARNIER: So this right here that I have highlighted
is the contents of Google Drive classes that you'll need
to use to access Google Drive.
That makes it easy for you to access Google Drive API.
It contains also the Libs folder, which contains
basically our dependencies that you also need to add in
the class path.
VIC FRYZEL: OK, so lets add these.
So if you're using an IDE like Eclipse or IntelliJ, this is
going to be a little bit different.
You should know how to add JARs to your class path.
If you don't though, we actually link in the
documentation on how to do it for each individual IDE.
So here you have the command all set to run to compile a
sample, except one step we didn't show is just
downloading the source code, like copying and pasting it
from the documentation.
So just show where that is really quick in the
documentation.
NICOLAS GARNIER: That is-- set of sample.
VIC FRYZEL: If we just scroll down here.
So this is actually the source code.
Nicolas--
NICOLAS GARNIER: Well, I'm going to actually create the
file right now using VI, using VIM.
So this is the sample for the project.
It's very simple.
It's only one class.
Inside the template, you're going to have to enter the
client ID and the client secret.
And then we'll also have to create an example file, which
is going to be called document.text, which is the
file that's going to be uploaded to Google Drive using
that sample using the Java code.
So let me just create that tile.
Just trying to get the name right.
All right, that's it.
VIC FRYZEL: So like you mentioned before, the only
thing we have to do to the source code now is paste in
our client ID and secret.
NICOLAS GARNIER: Yeah, I'm also going to create, very
quickly here, the example file, which is going to be
documents.text.
And what do you want in there, Vic?
VIC FRYZEL: Hello World.
NICOLAS GARNIER: Hello World, for a change.
All right, that's it.
VIC FRYZEL: So let's just go back.
So here, you can see that we have two lines where we place
our client ID and our client secret.
These are in the API console project that we just created.
NICOLAS GARNIER: So this is the client ID?
VIC FRYZEL: Mm-hm.
NICOLAS GARNIER: And now the client secret.
And then I save and quit.
VIC FRYZEL: Yep.
OK, and then let's just go back to the previous command
where we added all of the JARs to the class.
NICOLAS GARNIER: All right, then what we're going to show
now is basically simply compiling that source file
that we just created and then running it
in the command line.
So I had the commands ready.
As you can see, we're setting the class pass correctly,
including the current folder, which contains the Google API
server's drive v2, rev5, 172-bit, a JAR, which
contains, as I said, the drive business subjects, basically,
and the Libs folder, which contains all our dependencies.
VIC FRYZEL: Yep.
NICOLAS GARNIER: All right.
VIC FRYZEL: So that worked?
No errors?
NICOLAS GARNIER: Perfect, no errors.
VIC FRYZEL: Now we just have to run it.
NICOLAS GARNIER: Now let's just run it.
Here is the Java command to run it.
All right.
So now we have to go through the out-of-band OAuth 2 flow,
which basically--
here's basically how it works.
We are asking the user to go to that particular URL, which
we printed on the screen here, which we're
going to do right now.
Then the user is going to say if he wants to allow access to
his Google Drive data to the test project or if he doesn't
actually want to allow access.
And here is how the out-of-band OAuth2 works is,
we give you the OAuth code here in the browser, and you
have to copy paste that into the application.
The application is using the code going through the OAuth
protocol, exchanging the code for tokens, then uploading the
file, and here you go.
And now we have the file ID.
The file has been uploaded to Google Drive.
So I'm going to go to my drive and search for--
let's say, a text file.
Maybe there's a bit of a delay.
What was the name?
Document--
VIC FRYZEL: Document.txt.
NICOLAS GARNIER: --.txt.
We're facing a bit of trouble.
VIC FRYZEL: This is unusual.
NICOLAS GARNIER: This is unusual.
Usually the file shows up right away in your drive.
So maybe the network agency that we're
having are causing this.
VIC FRYZEL: So refresh.
NICOLAS GARNIER: Lets just do a full refresh.
As you can see we're having a very slow connection.
VIC FRYZEL: Yeah.
NICOLAS GARNIER: All right, it's not there.
VIC FRYZEL: You lied.
NICOLAS GARNIER: I lied.
I totally lied.
If you want--
VIC FRYZEL: Did you use the right account?
NICOLAS GARNIER: I did.
There's only one account.
So if you want, I'm going to get the file idea.
And do you want me to use the OAuth 2 Playground maybe to
show the actual file has been uploaded to Drive?
We could.
VIC FRYZEL: Sure.
NICOLAS GARNIER: Yeah?
So if you guys know the OAuth 2 Playground--
sorry.
Playground is a tool that allows you to play around with
APIs that use the OAuth 2 protocol.
It lets you go through your protocol.
This is the OAuth 2 web server flow.
We're just enabling the Playground to access the
Google Drive API for us on my accounts, exchanging the code
for tokens.
And here is a little convenient tool to allow you
to select the right REST operation.
And we want to actually get the file that
we've uploaded to Drive.
We can see the file has been uploaded.
VIC FRYZEL: Yes, it's definitely there.
The title is My Document, though.
VIC FRYZEL: Oh, whoops.
NICOLAS GARNIER: Which is probably a little issue in our
Java sample, which we'll fix later on.
VIC FRYZEL: Actually, for what it's worth, I think all of the
samples are called My Document.
NICOLAS GARNIER: Oh, OK.
VIC FRYZEL: So maybe our search was just incorrect the
entire time.
NICOLAS GARNIER: All right.
VIC FRYZEL: Whoops.
NICOLAS GARNIER: We can see the My Document file has been
uploaded here.
VIC FRYZEL: Yeah, good job, Nicolas
NICOLAS GARNIER: Yeah, awesome.
This totally worked exactly as expected.
So I'm just going to open the file.
You can see the ID here on the URL, which is
with the correct ID.
VIC FRYZEL: Next time we'll have better network
connection.
NICOLAS GARNIER: Yes.
VIC FRYZEL: Oh, there we go.
NICOLAS GARNIER: Now we can see the
Hello World file, awesome.
VIC FRYZEL: Yeah.
Success.
NICOLAS GARNIER: Success, boom.
VIC FRYZEL: Massive excitement.
NICOLAS GARNIER: Yes.
VIC FRYZEL: So now we can go through the source code a
little bit and talk through, line by
line, what we're doing.
NICOLAS GARNIER: OK.
VIC FRYZEL: So, pretty much what we start off doing is
setting up authorization.
This is something that you have to do in pretty much
every interaction you have with the Drive SDK.
So authorization varies language by language.
But here, we're creating the Google authorization code flow
and just giving it a few parameters.
Most of these parameters you don't really ever have to
modify except for the client ID and client secret.
And that's pointed out pretty directly at
the top of file there.
And if you were writing a web application, you would also
have to modify the redirect URI.
NICOLAS GARNIER: Yeah, you wouldn't really use this
out-of-bound flow.
If you were billing a web app, you would actually use
probably the web service flow or the client side flow, which
is a flow for JavaScript application, which includes,
as I've just shown, using the OAuth2 Playground.
Your app would handle the redirect back and forth to the
grant page, which makes it way better for users than having
to copy paste this URL.
VIC FRYZEL: Yep.
So once we set up the authorization flow, we
actually initialize it and then run a request to Google
to get an authorization URL, which the user then
has to browse to.
And once they browse to that, that's when they get the
authorization code and copy and paste that back to us.
So you can see here in the next five lines that we're
pretty much just--
NICOLAS GARNIER: This is all done here.
VIC FRYZEL: Yeah, getting the URL, telling the user to
browse to it, and then clicking the code back.
NICOLAS GARNIER: Yeah, so when the user copy/pastes that code
back, we take it, and we exchange it for tokens, so an
access token and a refresh token.
And we get this credential object here on that line,
which contains actually the access token and the refresh
token that we got back from Google.
And then you can use that to initialize your Drive service.
That's this line.
And then, very simply, it's a few lines.
You create the file, which actually correctly has the
title My Document.
I'm so sorry.
I really thought we were giving the name
of the actual file.
So my search was wrong, but the sample worked as expected.
And then we simply also read the actual file itself, the
content of the actual file, and then insert the file.
It's like this one command line.
Insert the file to Google Drive, giving it the metadata
that we've created here and the file content.
VIC FRYZEL: Yep.
OK, so now that we've walked through that, we can talk
about other ways that you might be able to extend this
to be more of a realistic application.
So there are few things that, if you were running a real
command-line application using the Drive SDK,
that you should do.
And not even just a command-line application, but
rather a desktop application.
The out-of-band flow is what you would use in order to do
authorization.
But having the user copy and paste a link into their
browser frankly is not a very good user experience, right?
And having them copy and paste the code back to your
application just doesn't make any sense to a user.
So what you should do is fire up a web server locally.
And then the redirect URI for an out-of-band OAuth flow
redirects the user back to local host.
That's a valid redirect URI.
So here you would redirect the user back to local host and
spawn up a web server on their computer and then receive the
authorization code in the request.
Once your web server receives that, you would shut the web
server down, and now you have the code that's needed.
So again, those steps are, one, start up a web server.
Two, open up the user's browser for them and send them
straight to that link.
When they click Allow Access, your web server will receive
the request.
And then close the browser, close the web server.
NICOLAS GARNIER: Or you could display a message to the user
like, hey, switch back to the application.
we've got the code now.
You can shut off your browser and go back to the--
VIC FRYZEL: Yeah, there are probably even fancy ways to
detect that you've received the grant and then close the
browser for the user and just come
straight back into context.
So that's what you would do in a more real-world context.
We don't do that here because this is a very
quick-start-oriented sample.
Also, if you were writing a real Drive integration on a
desktop application, you would do much more
than upload, right?
You would probably at least download files.
NICOLAS GARNIER: Yeah, list files, download content,
everything you can do through the Drive API, change the
sharing of files, search for files.
VIC FRYZEL: Yeah, so we list complete Java samples on how
to do every single thing in the API in the reference
documentation, which is linked to in the left half of our
documentation.
NICOLAS GARNIER: If you're building also a web
application and want to use the server side flow, we have
a really good sample on our authorization documentation
page online.
So invite you to go check it out.
VIC FRYZEL: We call that sample Dr.Edit.
It's our flagship web application sample.
And we have that in every language as well.
So I think that's pretty much it.
NICOLAS GARNIER: Yep.
VIC FRYZEL: Anything else you'd like to add?
NICOLAS GARNIER: I think that's it as well.
VIC FRYZEL: All right.
Well, thank you so much for joining.
We're super glad to have you.
I'm Vic Fryzel, and this is Nicolas Garnier.
NICOLAS GARNIER: Thank you, everyone.
VIC FRYZEL: And we'll see you next time.
Bye.
NICOLAS GARNIER: Bye-bye.
[MUSIC PLAYING]