Welcome to Google I/O 2013. I am Prashant Labhane from
the Google APIs team. And today we'll be learning
about getting started with Google APIs in App Engine
using Python. In Google APIs' team, we try
really hard to make tools so that your application
development is easier and you can use the APIs very easily. In today's session, we'll be
covering about running a Quickstart application
locally. We'll deep-dive into the
Quickstart application. We'll do application development
using an API. We'll deploy the application
to App Engine. And at the end, I'll provide
some reference. But before getting started,
the first question I ask before writing any application
is to see what is provided in the APIs. You can do that without writing
a single line of code with a tool called
APIs Explorer. You can go to that tool
at developers.googl e.com/apis-explorer. And as you can see, Google
provides a lot of APIs, some of them very product-specific
like Blogger and Books. But we also provide a lot of
infrastructure APIs like Cloud Storage and Compute Engine. For today's session, I'm going
to be using one of my favorite APIs, Google+. But I would encourage you to try
out this exercise with any API of your liking. So as you can see, Google+
provides resources like activities, comments, and people
information in the API. So there's a method called a
Search in which you can query anything, and you will get
results for the activities that are relevant
to that query. So as you can see here, it
gives a bunch of items-- the title, the URL, which
is basically a list of activity resources. What you can also do with the
Google+ API is to get information about a particular
users list of activities. And you can do that using
plus.activities.list. But before we get into that,
we need to authorize the application to get access
to the user's data. We do that using OAuth 2. So I'm going to turn on
OAuth 2 and allow access for Google+ API. And this presents me a
user content screen. During the course of this
session, you'll find the screen come up again. What this is trying to do is
trying to authorize the application Google APIs Explorer
to access user Prashant Labhane's data
for the Google+ API. I'm going to give it access. And then I can use
my user ID here. For brevity, you can use Me as
the code word for user ID. And it will pick up the user
from your browser. And I want to see whatever
I have shared publicly. Now, as you do that, you will
see, again, the same list of activities. But this one is particular to
the activities that are shared by the user. Looking at this API, I have a
good sense of what application I want to build. So I will be building an
application which displays the URLs of all the activities
for a particular user. So we can now get started with
the first section, which is running your Quickstart
application. For the purpose of this
exercise, I'm using a brand new Google account
called Google I/O 13 Python [? dev ?]. So all the flows you'll be
seeing in today's session will be first-time session. If you already have registered
with App Engine or APIs Console, your flow might look
a little bit different. So let's get started. The first thing you need
to do is to register a Google Cloud Project. And you can get an instance
of Google Cloud Project at cloud.google.com. Since this is the first time
it's going to ask me for a verification-- and I'm going to go ahead
and complete the verification flow. So as you can see in the
previous screen, I had to get a verification code. You can get that via a phone
call or a text message. I just got my verification
code, which I'm going to verify. And that brings me into the
Google Cloud Console. I'm going to agree to the
terms of service. And I'm going to create
a new Cloud Project. I'm going to call it Google
APIs Quickstart. By default, the Cloud Project
comes with a project ID. But you can also choose
something that you like. I'll try to see if I can get
something of my liking. So I'll try Google APIs
Quickstart, which already exists, so I'm going to say,
Google I/O 13 Quickstart. And that would be the
name of the project that we'll be using. So it's going to create a new
Cloud Project at this point. While we are doing that, let's
also download the App Engine SDK, which we'll be using
later in this session. You can download the
App Engine SDK from developers.googl
e.com/appengine/download. You can also find
the link in the presentation for this video. I'm using a MacBook. And it already has
Python installed. I am going to use Hands,
the App Engine SDK. In the meantime, the Cloud
Project has been created. The name of the project is
Google APIs Quickstart. And the project ID is Goog
I/O 13 Quickstart. The Google Cloud Project is the
home for a lot of other Google technologies like Google
Cloud Storage, Cloud SQL, Bit Query, which I would
encourage you to try after this session. Now my Google App Engine
SDK is downloaded. I am going to extract
the file. And I am going to move it to
my Applications folder. When you install the Google App
Engine Launcher, you also want to make sure-- let me change my system
preferences. OK. When you use Google App Engine
Launcher, you also want to make sure that you create the
right symlinks so that you'll be able to use the SDK tool
from command line. So this is what it does the
first time it starts up. If you happen to miss the step,
you can always go back to Google App Engine Launcher
to make symlinks, and it'll create the links for you. OK. Now we can get started
with the next step. Google provides a set of client
libraries to make it easy for you to use APIs, though
most of the APIs are resource-oriented and you can
always write your libraries around the protocol. And we encourage people to use
the client libraries, because they are easier to use and they
also encompass a lot of functions that you will see. You can access the client
libraries at developers.googl e.com/api-client-library. As you can see, Google provides
libraries in very many languages. For today's session, we'll
be using Python. Google Client libraries make it
very easy to take care of a lot of your application tasks
like authentication batching, logging, et cetera, so that you
can focus more on writing the code for the API, as you
will see in the next sections. What we also provide, in
addition to the client library, is a way to get you
started quickly on your project with the Quickstart
widget. So in this case, as we
discussed, I'm going to be using the Google+ API. I'm going to be deploying
it on Google App Engine. And I'm going to configure
my Cloud Project with this project. So as you see, it's already
pre-populated with Google APIs Quickstart, which is the Cloud
Project we just created. Please make sure you read
the terms of service. This one is the terms of service
for the Google+ API. And it's going to finish
the widget. What you get at the end of
Quickstart is your starter application as well as the
client credentials that go with it. So I'm going to download
both of these. And as you can see, in my
Downloads folder I have the Client Secrets file as well
as the App Engine Project. So let's take a look at the
App Engine Project. The App Engine Project comes
with a default Client Secrets. And if you take a closer
look, the Client Secrets file is empty. It doesn't have the client
ID or the Client Secret. What you could do is, from the
developer's console, paste in the client ID and the
Client Secret. But the method I prefer to use
is to just drop the file into the folder. And now, to make it easy for
this session, I'm going to rename this folder to be called
My App Engine Project so that we can refer
to it more easily. OK. Now that I have the new Client
Secrets into the folder, let's go to the next step, which
is to run this Quickstart application locally. As you can see, we have
downloaded the App Engine SDK and we have created symlinks. And the App Engine SDK comes
with a lot of handy tools to run application. One of them is called the
Absorber that lets you run an App Engine application locally
on your machine. So I'm going to go to the
Downloads folder, and it has my App Engine Project. I am going to run dev
app server against my App Engine Project. And it's going to start up a
server that you can access on local host 8080. So this is your application
running locally. The first screen that you see
here is asking for the user authentication to
the application. Since this is a locally-running
application, it does not require
any log-in. So it's a test log-in screen. The next screen that you see
is the OAuth screen that we previously saw. But in this case, the service
account for the project is asking to authorize that
application to get the data for Prashant Labhane
for Google+ APIs. I'm going to say, Accept. And it's going to
take a while. And with that, we have a
locally-running application. As you can see, the application
doesn't do much but render HTML. It gives you handy links
on how to use the Google+ API in Python. With that, we have our
application running locally. Let's move to the next section,
where we'll do a deep-dive of the Quickstart
application code, what it does. And we'll do a bit of
application development later. So let's get into the deep-dive
of the code. As you can see, the
project is here. You can use an ID of any choice
to import this project. In my case, I'm going to just
use my text editor. So the main file that controls
your application in the Quickstart project is main.pi. And let's do a code walk-through
of what that application does. The first thing you would notice
that it imports the Client Secrets file that we
put in the same folder. It's creating a service
for your API. And this is the service that
you can use to invoke resources and methods upon. It creates an OAuth-decorated
blazed on the Client Secret file. And it has a main handler and
Get method, which is decorated with OAuth Required. The decoration OAuth Required
makes sure that the OAuth flow is called before the Get
method is called. Since this is a simple
application, we will be using OAuth Required. However, when you're developing
your complex application, you want to make
sure that the content screen shows up at the right time, in
which case I would encourage you to try OAuth Aware, which
lets the application take control of when the
OAuth credential gets checked and called. And as you can see, the
application is writing out a simple HTML. What you will also notice is
the application is a WSGI application, which comes
with Main Handler. But it also comes with the OAuth
2 callback handlers, which take care of OAuth flow
on behalf of you in the application. You can also register other
handlers as and when needed for this WSGI application. Now that we have a good sense of
what the Quickstart code is doing, we can now get started
with coding our application. So I already walked you through
the OAuth flow. And we did a code
walk-through. Let's get to the next section,
which is application development. What you can see is the
locally-running application gives you a handy link to
the Python Google+ API. So let's take a look. As you can see, the same
resources that we saw at the start of this session for APIs
Explorer are now exposed as Python methods that can
be called upon. So let's take a look
at Activities. And you can see the same methods
like Search and List can be called upon
in this API. So let's go ahead and
write some code. So what I'm going to do is I
mirrored the same activities that we did earlier with the
APIs Explorer in Python code. So I'm going to get Activities
results. I'm going to do it using
the service. And as you can see, the service
is created here for the Google+ API. If you're using a different API,
then you can invoke the methods relevant to that API. So I'm going to call
service.activities. And as you can see,
the Activities has a search method. So I'm going to call Search
using the query, Google. And now I'm going to execute
this command. Another thing to notice is for
executing any API call, you need to make sure you get an
authenticated HTTP object to carry that API call over. So in this case, it's
already decorated. I'm going to use the HTTP object
from the Decorator. So it's going to be
decorator.http, which is the authenticated HTTP connection. And the next thing I'm going
to do is I'm going to write out Activities Result
in the HTML. And remove all this code. OK. And now I'll make sure
I save the file, because that's important. One of the good things about
developing with dev app server is it automatically takes care
of any code changes you make. So you don't have to go and
kill your application and restart it. But you can keep you dev app
server running, keep developing. And you will be able to see the
changes without having to restart dev app server. So in this case, as you can see,
I modified my code and it's giving me a list of Python
objects relevant to the query, Google. What I'm also going to do next
is to get a list of activities for my user account. As you can recall, in APIs
Explorer we had to turn on OAuth 2. But this application is already
decorated with OAuth 2 Required, which means you can
call the List method for the Activities in my account, and
the Decorator OAuth 2 Required will take care of running
OAuth 2 on your behalf. So I'm going to use List. User ID equals Me, and
Collection is public. And I'm going to execute it the
same way I executed the Search, using the Decorator's
authenticated HTTP object. If you ever find yourself not
knowing what the parameters are, you can always go to the
documentation for the API and check out what the query
parameters are. Now, again, as I said earlier,
when you change your code, just make sure you save your
file, and you'll be able to see the changes quickly. So this is the list of
activities that is shared by my user account. What you can also do in App
Engine Python is to use templates to render the
HTML in a nice way. As I said earlier, we'll be
creating links for the various sets of activities. The Quickstart application comes
with a built-in template that you can use to
render these. In Python 2.5, it does
Django templates. But if you are using Python
2.7, that would be [? Django. ?] So let's write a small
template file to render the results. So let's create an HTML file. And I'm going to write
a Django template. And I know that I'm going to
be getting items in this object, as you have seen
in the APIs Explorer. And also, you can inspect this
code, which tells you that it's a list of items and each
of the items has a URL and a title, which is relevant
to the activities. So this is what my template
is going to do. So I'm going to get the
link from Items URL. And I'm going to be getting
the item's title. And I'm going to put
some line breaks. And that is a very simple
template that I'm going to be calling activities.html. And now that I have the
template, I can go to my code. And I'm going to invoke the
template and render my Activities result object
using this template. So I'm first going to declare
the template path. And as you can see, the
activities.html file is in the same folder, so I'm going to be
using the directory name. And joining with
activities.html. And next up, I'm going to be
rendering the template path, which is the template that
it just wrote with the Activities result. If you are wondering where the
template model came from, you can see, as I told earlier,
the Quickstart application comes with the default
template. And with that, I'm going
to save my file. And as you can see, it now
renders a link to the activities that are shared
by my user account. And if you click on them, you
will go to the specific activity post that
is relevant. Now, with that, I am pretty
happy with my application. And I want to deploy
it to App Engine. So we can get started with the
next section, which is deploy to App Engine. Before we do that, let's
understand a little bit about client credentials. The client credential file,
which I downloaded earlier, client_secrets.json, was to run
the application locally. To deploy the application on App
Engine, we need to get the credentials for a web
application on App Engine. You can do that by visiting
developers.google.com/console. And go to API access. And just create a
new client ID. In this case, it's a
web application. The site name is your Cloud
Project ID name. And that is also the name of
your App Engine application. And it will be hosted
on appspot.com. So your site name would be
Goog I/O 13 Quickstart. I'm going to go to my Cloud
Project and check it again. It's Goog I/O 13 Quickstart. So the application
name is correct. And it's going to be hosted
on appspot.com. And like that, you create
a new client credential. What you also want to do is
once you've created this client credential, you want to
make sure that the application still continues to
run locally. To do that, you can edit your
settings and add Local Host to the redirect URIs, which make
sure that the application continues to run locally. However, when you are finally
deploying your application, I would recommend moving this
out so that it's safer. What I'm going to do, now that
I have created a client credential, I'm going
to download the Client Secrets JSON. And as you can see here,
I have a new Client Secrets file. So I'm going to remove the old
one and move the new Client Secrets into the project. The second thing that you want
to do is to configure your application on how the code
is deployed to App Engine. In Python, that's controlled
by a file called ab.yamal, which comes in your Quickstart
project. You want to enter your
application ID, which is Goog I/O 13 Quickstart in our case. What you also want to do is I
prefer to be secure and always use HTTPS for my applications. And you can do that using the
parameter Secure Always. If your Quickstart application
code doesn't come with Secure Always, I would recommend
you adding this. And I'm going to
save this file. And now we are ready
to deploy this application to App Engine. As I said earlier, I'm going to
stop my local application. The App Engine SDK comes with
a variety of handy tools. One such tool is AppConfig,
which lets you upload your application to App Engine. So I'm going to update, and
I'm going to give it the location of my App
Engine project. And the first time it runs, it's
going to ask you about the account with which you have
your Cloud Projects and things like that. So I'm just going to
enter my password. And with that, this
is the first run. It's going to take some time to
deploy the application that I just wrote locally
into App Engine. And it seems like
it's completed. So I'm going to go to
my browser and go to googio13quickstart.appspot.com. And it's going to ask
me to log in. So this is the part where the
user is authenticating to the App Engine application. This is the second screen, which
is the OAuth screen. Now, as you can see, this
is showing up again. This is because this time the
application is different. In the previous case, you were
running your application locally, and that's the
application you authorized. Now that you have deployed it
to App Engine, you want to authorize the App Engine
application and the authorization to access the
data for Prashant Labhane. I'm going to go and
accept that. And with that, you have the same
application that you just developed locally and deployed
onto App Engine. Now, with that, we come to
the end of this session. But I wanted to give you some
handy references and things that are happening
in Google APIs. If you are more interested, I
would definitely recommend you to check out Google Cloud
Endpoints, which enables third parties to host APIs
on Google. Now, this is a very
powerful concept. And we are trying to bring
the client libraries and authentication and all the good
stuff that you just saw in this session to third-party
APIs as well, so that as an application developer, you can
use Google APIs as well as you can use third-party APIs to
create really powerful applications on App Engine. If you are interested in the
Java version of this talk, I would recommend you watching
[INAUDIBLE] on getting started with Google
APIs in Java on App Engine. And I would also recommend you
to going to some of the reference, more specifically
developers.google.com and cloud.google.com to check out
all the different technologies that are offered in
Google Cloud. With that, we come to the
end of this session. Thanks for watching, and have
a good time coding.