25.09.2014       Выпуск 45 (21.09.2014 - 28.09.2014)       Статьи

Простой доступ к Google API

Обновленный рецепт доступа к API Google Application Engine от автора достаточно популярной в свое время книги "Core Python Applications Programming" ( «Python: создание приложений. Библиотека профессионала») Уэсли Чан


Экспериментальная функция:

Ниже вы видите текст статьи по ссылке. По нему можно быстро понять ссылка достойна прочтения или нет

Просим обратить внимание, что текст по ссылке и здесь может не совпадать.


You can also watch a video walkthrough of the common code covered in this blogpost




Back in 2012 when I published

Core Python Applications Programming, 3rd ed.

, I


about how I integrated Google technologies into the book. The only problem is that I presented very specific code for 

Google App Engine



 only. I didn't show a generic way how, using pretty much the same boilerplate Python snippet, you can access any number of Google APIs; so here we are.

In this multi-part series, I'll break down the code that allows


to leverage Google APIs to the most basic level (even for Python), so you can customize as necessary for your app, whether it's running as a command-line tool or something server-side in the cloud backending Web or mobile clients. If you've got the book and played around with our

Google+ API

example, you'll find this code familiar, if not identical — I'll go into more detail here, highlighting the common code for generic API access and


bring in the G+-relevant code later.

We'll start in this first post by demonstrating how to access public or


 data from Google APIs. (The next post will illustrate how to access


data from Google APIs.) Regardless of which you use, the corresponding boilerplate code stands alone. In fact, it's probably best if you saved these generic snippets in a library module so you can (re)use the same bits for any number of apps which access any number of

modern Google APIs


Google API access

In order to access Google APIs, follow these instructions:

  • Go to the Google Developers Console and login.
    • Use your Gmail or Google credentials; create an account if needed
  • Click "Create Project" button
    • Enter a Project Name (mutable, human-friendly string only used in the console)
    • Enter a Project ID (immutable, must be unique and not already taken)
  • Once project has been created, click "Enable an API" button
    • You can toggle on any API(s) that support(s) simple API access (not authorized).
    • For the code example below, we use the Google+ API.
    • Other ideas: YouTube Data API, Google Maps API, etc.
    • Find more APIs (and version#s which you need) at the OAuth Playground.
  • Select "Credentials" in left-nav under "APIs & auth"
    • Go to bottom half and click "Create new Key" button
    • Grab long "API KEY" cryptic string and save to Python script

You can also watch a video walkthrough of this app setup process in the "DevConsole"



Accessing Google APIs from Python

Now that you're set up, everything else is done on the Python side. To talk to a Google API, you need the

Google APIs Client Library for Python

, specifically the


function. Download and install the library in your usual way, for example:

$ pip install -U google-api-python-client
NOTE: If you're building a Python App Engine app, you'll need something else, the Google APIs Client Library for Python on Google App Engine. It's similar but has extra goodies (specifically decorators — brief generic intro to those in my previous post) just for cloud developers that must be installed elsewhere. As App Engine developers know, libraries must be in the same location on the filesystem as your source code.

Once everything is installed, make sure that you can import



$ python Python 2.7.6 (default, Apr  9 2014, 11:48:52) [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import apiclient.discovery >>>



is the


function, which is what we need to create a service endpoint for interacting with an API. Now craft the following lines of code in your command-line tool:

from apiclient.discovery import build API_KEY = # copied from project credentials page SERVICE = build(API, VERSION, developerKey=API_KEY)

Take the API key you copied from the credentials page and assign to the


variable as a string.


, embedding an API key in source code isn't something you'd so in practice as it's

not secure

whatsoever — stick it in a database, key broker, encrypt, or at least have it in a separate byte code (




) file that you import — but we'll allow it now solely for illustrative purposes of a simple command-line script.

In our short example we're going to do a simple search for "python" in public Google+ posts, so for the 


 variable, use the string


. The API version is currently on version 1 (at the time of this writing), so use




. (Each API will use a different name and version string... again, you can find those in the

OAuth Playground

 or in the docs for the specific API you want to use.) Here's the call once we've filled in those variables:

GPLUS = build('plus', 'v1', developerKey=API_KEY)

We need a template for the results that come back. There are many fields in a Google+ post, so we're only going to pick three to display... the user name, post timestamp, and a snippet of the post itself:

TMPL = '''     User: %s     Date: %s     Post: %s '''

Now for the code. Google+ posts are activities (known as "notes;" there are other activities as well). One of the methods you have access to is


, which lets you


public activities; so that's what we're going to use. Add the following call using the


service endpoint you already created using the verbs we just described and execute it:

items = GPLUS.activities().search(query='python').execute().get('items', [])

If all goes well, the (JSON) response payload will contain a set of


 (else we assign an empty list for the


loop). From there, we'll loop through each matching post, do some minor string manipulation to replace all whitespace characters (including NEWLINEs [


]) with spaces, and display if not blank:

for data in items:     post = ' '.join(data['title'].strip().split())     if post:         print TMPL % (data['actor']['displayName'],                       data['published'], post)

We're using the


statement here in Python 2, but a pro tip to start getting ready for Python 3 is to add this import to the top of your script (which has no effect in 3.x) so you can use the


function instead:

from __future__ import print_function


To find out more about the input parameters as well as all the fields that are in the response, take a look at

the docs

. Below is the entire script missing only the


which you'll have to fill in yourself.

#!/usr/bin/env python
from apiclient.discovery import build
TMPL = '''     User: %s     Date: %s     Post: %s '''
API_KEY = # copied from project credentials page GPLUS = build('plus', 'v1', developerKey=API_KEY) items = GPLUS.activities().search(query='python').execute().get('items', []) for data in items:     post = ' '.join(data['title'].strip().split())     if post:         print TMPL % (data['actor']['displayName'],                       data['published'], post)

When you run it, you should see pretty much what you'd expect, a few posts on Python, some on Monty Python, and of course, some on the snake — I called my script



$ python plus_search.py 
    User: Jeff Ward     Date: 2014-09-20T18:08:23.058Z     Post: How to make python accessible in the command window.

    User: Fayland Lam     Date: 2014-09-20T16:40:11.512Z     Post: Data Engineer http://findmjob.com/job/AB7ZKitA5BGYyW1oAlQ0Fw/Data-Engineer.html #python #hadoop #jobs...

    User: Willy's Emporium LTD     Date: 2014-09-20T16:19:33.851Z     Post: MONTY PYTHON QUOTES MUG Take a swig to wash down all that albatross and crunchy frog. Featuring 20 ...

    User: Doddy Pal     Date: 2014-09-20T15:49:54.405Z     Post: Classic Monty Python!!!

    User: Sebastian Huskins     Date: 2014-09-20T15:33:00.707Z     Post: Made a small python script to get shellcode out of an executable. I found a nice commandlinefu.com oneline...


: To test your skills, check the docs and add a fourth line to each output which is the URL/link to that specific post, so that you (and your users) can open a browser to it if of interest.

If you want to build on from here, check out the


app using the Google+ API featured in Chapter 15 of the book — it adds some brains to this basic code where the Google+ posts are sorted by popularity using a "chatter" score. That just about wraps it up this post. Once you're good to go, then you're ready to learn how to perform

authorized Google API access

in part 2 of this two-part series!

Разместим вашу рекламу

Пиши: mail@pythondigest.ru

Нашли опечатку?

Выделите фрагмент и отправьте нажатием Ctrl+Enter.

Система Orphus