Professional Documents
Culture Documents
13 Understandinggoogleapis-130318134725-Phpapp01
13 Understandinggoogleapis-130318134725-Phpapp01
Google APIs
Building application that uses Google APIs
Fethi DILMI
Active Member at Scientific Club of ESI – CSE
Technical Manager at GDG Algiers
Microsoft Student Partner
What's Google APIs?
What's Google APIs
●
Google offers a variety of APIs, mostly web APIs for
web developers and mobile developers.
●
The APIs are based on popular Google consumer
products, including Google Maps, Google Earth,
AdSense, Adwords, Google Apps and YouTube.
What's Google APIs
Example:
●
YOU use Google+ from your web browser.
●
Your Android application uses Google+ through
Google+ API.
●
i.e: Google APIs are the tools we need to build
applications that can use Google Products.
How Google APIs work behind
the scenes?
How Google APIs work behind the scenes ?
●
Most of Google APIs are web-based APIs.
●
This kind of APIs are called RESTFUL APIs (because they
are based on REST architecture).
●
REST is a style of software architecture that is based on
HTTP protocol to retrieve data.
How Google APIs work behind the scenes ?
●
Most of Google APIs are web-based APIs.
●
This kind of APIs are called RESTFUL APIs (because they
are based on REST architecture).
●
REST is a style of software architecture that is based on
HTTP protocol to retrieve data.
Simply, in order to use Google APIs , you
only need to make HTTP requests to get
data ☺
How Google APIs work behind the scenes ?
1- API Scope:
●
A scope is the main part of the HTTP request.
●
In our case it's: https://maps.googleapis.com/maps/api/place
●
A scope defines the web address of the API.
●
For example, the scope of Google Latitude API is:
https://www.googleapis.com/latitude/
2- Output formats:
●
There are 2 possible output formats for an API request.
– JSON
– XML
●
In the previous example, we could get the same results in
JSON format:
https://maps.googleapis.com/maps/api/place/nearbysearch/json ?
location=36.825,2.3257&radius=50000&sensor=false&key=AddYourOwnKeyHere
Global Structure of an API HTTP request
3- ACTION:
●
Each Google web API gives you a set of possibilities
called ACTIONS.
●
In our example, we specified for the Google Places API
the action “nearbysearch” to search places in a radius
of 50Km.
●
We could also search a place's detail.
Global Structure of an API HTTP request
4- Parameters:
●
Each action has a set of parameters.
●
Action Parameters let you customize the results you
want to get.
●
In our example, we could add the parameter
“type=food” to search only for restaurants.
Types of Google web APIs
Types of Google web APIs
●
There are 2 kinds of Google web APIs:
– Public APIs.
– Private APIs.
Types of Google web APIs
1- Public APIs
●
Interact with public content: Google Maps API, Google
Places API ..
●
Need an authentication key to be able to retrieve data.
Types of Google web APIs
2- Private APIs
●
Interact with user private date: Google+ API, Google
Latitude API, Google Drive SDK ..
●
Need an authorization process before accessing to user
data.
Public APIs and Authentication:
What's THAT !!
Authentication
●
Public APIs use authentication key to identify your application.
●
This means, in our previous example we would not be able to
make a search using Google Places API without specifying an
authentication key.
●
Each device type has a different kind of key:
– Android application authentication key.
– Web application authentication key.
– Web Service authentication key
– ..
Authentication
But Why ?
Authentication
●
Identify from which device your application is making API request:
i.e: You can't make an API request from a web browser using an
Android application authentication key.
●
Limit the quota of requests per day: Each API has a limited number
of requests per day. Since your application makes request using an
authentication key, Google Servers will be able to stop your
application when it exceeds its daily quota.
●
Limit the number of requests per second for a single user: Your
application may be used by millions of people at the same time, and
since we're talking about a daily quota, we have to limit the number of
requests/second for a single user.
Authentication
●
Identify from which device your application is making API request:
i.e: You can't make an API request from a web browser using an
Android application authentication key.
●
Limit the quota of requests per day: Each API has a limited number
of requests per day. Since your application makes request using an
authentication key, Google Servers will be able to stop your
application when it exceeds its daily quota.
●
Limit the number of requests per second for a single user: Your
application may be used by millions of people at the same time, and
since we're talking about a daily quota, we have to limit the number of
requests/second for a single user.
Authentication
●
Identify from which device your application is making API request:
i.e: You can't make an API request from a web browser using an
Android application authentication key.
●
Limit the quota of requests per day: Each API has a limited number
of requests per day. Since your application makes request using an
authentication key, Google Servers will be able to stop your
application when it exceeds its daily quota.
●
Limit the number of requests per second for a single user: Your
application may be used by millions of people at the same time, and
since we're talking about a daily quota, we have to limit the number of
requests/second for a single user.
Private APIs and Authorization:
What's THAT !!
Authorization:
●
Private APIs try to fetch user data.
●
This cannot be done without the permission of the user.
●
So we need a tool to demand permissions from the user
in order to perform action on his/her private data.
Authorization:
●
Private APIs try to fetch user data.
●
This cannot be done without the permission of the user.
●
So we need a tool to demand permissions from the user
in order to perform action on his/her private data.
???
You could ask the user for their name and password.
NO !!
But then the user has given your application access to all their data on that
service. That's not safe. Don't do that.
NO !!
The user's name and password are like keys to their digital kingdom, you
should never ask for them.
Better ☺
What we really want is a special key, one that only allows access to a
limited set of data in the API.
Better ☺
A special key that the User can let the App acquire and use without the use
of their name and password.
That's OAuth2.0 ☺
But for that to work, everyone has to confirm that everyone else is
who they say they are.
That's OAuth2.0 ☺
But actually, it's a little more complicated than even that, because that
special key (Code)
That's OAuth2.0 ☺
●
Register your organization like shown in the image and click submit:
How to create Authentication and
Authorization keys ?
● Agree & Accept
How to create Authentication and
Authorization keys ?
●
You can now notice that the Google Places API is activated:
●
Click on "Overview", then click on "Register" in order to register
your project:
How to create Authentication and
Authorization keys ?
●
Type a unique project ID
How to create Authentication and
Authorization keys ?
●
You've created automatically an authentication key for browser
applications
How to create Authentication and
Authorization keys ?
●
You can click on:
– Create New Server Key: To create an authentication key for
a server application
– Create New Server Key: To create an authentication key for
an Android application.
●
You can create many authentication keys for the same
application type (example: 3 authentication keys for
Android Applications)
How to create Authentication and
Authorization keys ?
●
And Now ..
How To Create
Authorization Keys ?
How to create Authentication and
Authorization keys ?
●
Click on “Create an OAuth 2.0 Client ID”. This dialog will show up:
How to create Authentication and
Authorization keys ?
●
Click on “Create an OAuth 2.0 Client ID”. This dialog will show up:
How to create Authentication and
Authorization keys ?
Now that we know what OAuth 2.0 looks like. How does it work in the
Google API Client for Python?
Credentials
Storage
And finally, because keys can change over time there is a Storage object
for storing and retrieving keys.
The Model
You set up and run a Flow, which in the end produces Credentials, which
you store in a Storage.
From Python
Later, when you need the key, you take it out of Storage and use it.
Step By Step
To use Credentials we retrieve them from the Storage and apply them
to an httplib2.Http() object.
Step By Step
user = users.get_current_user()
storage = StorageByKeyName(
Credentials, user.user_id(), 'credentials'
)
credentials = storage.get()
http = httplib2.Http()
http = credentials.authorize(http)
Now any HTTP requests made with http will be authorized with those
Credentials.
Thanks everyone ☺
References
●
“OAuth 2.0 and the Google API Client for Python”.
●
“Understanding Google APIs” :
http://fethidilmi.blogspot.com
●
Google Developers portal:
http://developers.google.com