[MUSIC PLAYING]
MALE SPEAKER 1: Shanghai GDG is a very interesting
development community.
FEMALE SPEAKER 1: I'm glad somebody
has asked this question.
MALE SPEAKER 2: This is where the magic happens.
FEMALE SPEAKER 2: This is primarily a question and
answer show, so if any of you out there
would like to ask questions.
VIC: Hello everyone.
Welcome back to Google Developers Live.
I'm Vic.
I work on the Google Drive SDK team.
Today I'm going to be going through how to work with Go
and the Google Drive SDK.
So what we're going to do is walk through our Quickstart
tutorial on writing a command line application in Google's
Go language so that we can upload a file to Google Drive
by running a simple command.
So here I have up the Google Drive SDK documentation,
specifically our Quickstart section.
And it enumerates all of the steps that you need to do in
order to get running.
This tutorial usually takes about five minutes or less.
I'll see how fast I can do it today.
But it pretty much involves four steps.
You're going to create an API console project in the Google
API's console, enabling the Drive API for that project.
You're going to install the Google Go Client Library.
You're going to set up the Go sample, and then you're going
to run the Go sample.
So if we come down here, the first step is to create an API
project in the APIs console, which is linked here.
I have it open.
So you can come up here and create a new project.
And I will call this Google Developers Live.
And if I create a project--
it just takes a second here.
And here we go.
So the first thing I have to do in the project is enable
the Drive API.
So let me just flip that on.
And that's it.
So now that I've enabled the drive API, I have to also set
up API access.
So what we're going to do here is set up our OAuth 2.0
credentials and allow the command line application to
actually access Google Drive after the user
authorizes a request.
So I'm going to click this big blue button here to create an
OAuth 2 client ID--
and Google Developers Live.
I don't have a product logo.
Click Next.
Now this is super important.
When you get to the client ID settings, you have to select
the application type of installed application, and not
web application.
Web application is a little bit different.
It's meant for our other flagship sample, which is the
DrEdit web application sample.
But we're not writing web applications today.
We're writing a command line application.
So that's an installed app.
And then it's not an Android or iOS app,
so we just use Other.
And then Create client ID.
And then that's it.
So right here, I have the client ID for installed
applications.
And here, you can see my client ID and secret.
So these are going to be important
in just a few minutes.
Once we've got that done, we're done in
the Google APIs console.
Come back to the documentation.
And the next step is to install the
Google Go Client Library.
I've already run these commands on my workstation
here, but I'm just going to run them again.
Nothing's going to happen.
So you can see, I've already got the Google Go API Client
Library installed.
So with that done, the next thing I'm going to do is
actually add the source code.
So I've got this source code copied and
pasted here into main.go.
If I take a look at this, you can see just a
bunch of source code.
There are two things that we have to change in this file.
If you download or copy and paste the tutorial code,
you'll see these two strings here, your client ID and your
client secret.
These are the only two things that you have to change in the
source code to get it to work for your application.
And what you do is come here to the Google APIs console and
copy your installed application client ID and
client secret.
And then you paste to those into the source code where
appropriate, here.
And that's it.
So everything else you can leave alone.
I'm going to leave the rest of source code alone for now.
And then we'll come back and look at that in a minute.
The other thing we're going to need to do before we can run
the sample is actually create a file to upload.
So here I've got document.txt.
And it says "hello world," so that's good enough.
And that's the file I'm going to upload.
So with that, the last thing to do is actually run the
sample, which we document right here.
It's a simple command, go run main.go.
So if I run this, the first thing the application is going
to do is ask me to browse to this URL.
And we'll talk a little bit more about what that means.
Essentially, we need to the application to have
an OAuth 2.0 token.
And it doesn't have one on the initial run.
So because it doesn't have one on the initial run, it asks us
as the user to browse to this URL in a web browser and then
click Allow Access.
What this is going to do is grant a token to our
application via this authorization code.
So we're going to copy this authorization code back into
the application, which is secure, waiting for our input.
And with that, the application is going to upload
document.txt up into Google Drive.
There it is.
We can see here that it was created with
this ID and this title.
So if that's the title--
let me go into Google Drive here.
And right now you see nothing here.
Let me refresh.
And here's our document.
If I open this up, you can see here that there's no preview
available, but this is our test document
that was just created.
So that's pretty much it for how the sample works.
Now let's take a look through the source code and see just
what's going on.
So if I come back here to the tutorial source code, the
first part of the source code, primarily, is a set of
imports, and then a configuration.
The configuration is somewhat important because that's where
you set your client ID and secret.
Everything else here is not super critical for getting
started with this command line application.
And then here what we're doing is generating the URL to visit
for authorization, which is this step here.
It's generating the URL that the user browses to to grant
an authorization code and get a token for
the subsequent requests.
So once we do that, we generate that and print it.
So go to the following link to your browser, and then there's
the authURL.
Once you print it, the user browses to it, and then we
receive some input from the user.
So we scan that line.
And the rest of this is just error checking, if the code
doesn't exchange, for a granted token, or
anything like that.
So now we create a new authorized Drive client.
This is very straightforward.
If there's an error, we show the error.
And then we create a new Drive file.
And we're going to set its title to "My Document," as we
saw before, and its description to "My test
document." With that done, we open
document.txt with a file pointer.
And then all we do is insert the file
using the Drive service.
And once that's done, we get a JSON representation of that
new file back, and all of its metadata, including things
like ID and title and description.
And here, we just want to print its ID and title.
So you can see that that's exactly what was printed out
here at the end of the program execution.
So that's the Go Quickstart sample.
Now once you've gotten to this point, there are some next
steps you could take.
So we have full support for obviously a number of other
features in Google Drive that you can access via the SDK.
So once you've gotten the hang of this, I recommend you maybe
extend this application to do something
like download a file.
Which is a little bit different.
When you download a file, you have to do more than just this
one line of code.
You have to let the user know--
let the user provide you which file to download.
And then you have to go and you have to fetch it, getting
its download URL, and then downloading the actual
contents of file locally.
So that's a little bit different.
You could also clean up the authorization mechanism.
So right now, as a user of this application, I have to
come in here and copy this URL, paste it into the web
browser, and then in a web browser, I have to click Allow
Access and copy the authorization code back into
the application.
That's pretty tedious for a user.
So instead what we could do is a fire up the web browser on
behalf of the user.
So for instance, we could open Google Chrome directly to the
authorization page.
And then we could start a local web server on the user's
computer in order to have the authorization page at Google
redirect back to the user's local computer.
And with the web browser running, we could actually
receive the authorization code straight from Google.
So this means that the user wouldn't have to paste this
authorization code into the application, but our web
browser that we have running locally would be
able to pick it up.
So that's something that a lot of people do with an installed
application.
It's not that hard to do.
You just have to start of the web browser for the user,
which there's plenty of frameworks to do.
On Linux, you could for instance use xdgopen, and then
you have to start the local web browser--
again, there's tons of ways to do this, and it's very
straightforward--
and receive the incoming request, and parse out the
authorization code.
And then you just continue as normal.
So essentially, when the user ran this application, they
would never see these lines.
They would only see their web browser pop up with the
authorization page.
And when they clicked Allow Access, you could here in your
application just continue on uploading the file.
So that's pretty much everything for the Go
Quickstart.
If you have any questions, please
find us on Stack Overflow.
Our tag is Google-Drive-SDK.
We're happy to answer anything that you come up with.
And if you want to take the next steps into something a
little bit more advanced, then definitely check out our
DrEdit sample applications.
Our DrEdit sample applications will allow you to write a web
app that integrates with Google Drive and exposes your
application in the Google Drive user interface, allowing
users to access different parts of your application to
Create New and Open With on a document right there.
So that's pretty much it.
Thanks very much, and thank you for watching.
[MUSIC PLAYING]