This is a 1-hr tech talk designed for Python developers. The session continues on a previous one providing an overview of using Google APIs, primarily those from Google Cloud (G Suite and Google Cloud Platform). It introduces new APIs or new features in APIs covered previously.
1. Exploring MORE Google
(Cloud) APIs with Python
Wesley Chun
Developer Advocate, Google
Agenda
1
Introduction
2
Google APIs
review
3
G Suite APIs
4
Hangouts Chat bot
framework
5
GCP APIs
6
Wrap-up
2. 01
Introduction
G Suite Dev Show
goo.gl/JpBQ40
About the speaker
● Developer Advocate, Google Cloud
● Mission: enable current and future developers to be successful using
Google Cloud and other Google developer tools, APIs, and platforms
● Videos: host of the G Suite Dev Show on YouTube
● Blogs: developers.googleblog.com & gsuite-developers.googleblog.com
● Twitters: @wescpy, @GoogleDevs, @GSuiteDevs
● Background
● Software engineer & architect for 20+ years
● One of the original Yahoo!Mail engineers
● Author of bestselling "Core Python" books (corepython.com)
● Teacher and technical instructor since 1983
● Fellow of the Python Software Foundation
7. The first word on Security
Authentication ("authn") vs authorization ("authz")
● authn: you are who you say you are
○ login & passwd
○ handprint authentication
○ retina scan
● authz: okay, you are who you say you are, but can you haz data?
○ OAuth2 - mostly authz, but some authn
○ Mostly about 3rd-party access to data
○ Users must give YOUR code access to THEIR data
○ Most of the time when you see "auth", it refers to authz
● Some refer to this as "consent" vs. "credentials…" which is which?
8. ● View application statistics
● En-/disable Google APIs
● Obtain application credentials
Using Google APIs
goo.gl/RbyTFD
Google APIs client
libraries; demos in
Others:
developers.google.com/
api-client-library
Python 2 or 3:
pip{3,} install -U
google-api-python-client
9. from googleapiclient import discovery
# Copy from project in DevConsole, or better yet, put into
# secure DB, encrypted store, credential server/proxy, etc.
API_KEY = ' ... '
# When using GCP APIs, we recommend OAuth2 service accounts!
# API information, i.e., (API='language', VERSION='v1')
SERVICE = discovery.build(API, VERSION, developerKey=API_KEY)
Simple API access
Authorized API access
OAuth boilerplate
goo.gl/KMfbeK
from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = ... # at least one (string or array of strings)
# 'storage.json' - where to store OAuth2 tokens from API
# 'client_secret.json' - OAuth2 client ID & secret (download from DevConsole)
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
# create API service endpoint; for example: API='sheets', VERSION='v4'
SERVICE = discovery.build(API, VERSION, http=creds.authorize(Http()))
10. OAuth2 or
API key
HTTP-based REST APIs 1
HTTP
2
Google APIs request-response workflow
● Application makes request
● Request received by service
● Process data, return response
● Results sent to application
(typical client-server model)
11. 03
G Suite APIs
G Suite
Collaborate and be more productive with G Suite
12. List (first 100) files/folders in Google Drive
from __future__ import print_function
from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
files = DRIVE.files().list().execute().get('files', [])
for f in files:
print(f['name'], f['mimeType'])
Listing your files
goo.gl/ZIgf8k
List (first 100) files/folders in Google Drivefrom __future__ import print_function
import os.path
import pickle
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient import discovery
creds = None
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
TOKENS = 'token.p' # where to store access & refresh tokens
if os.path.exists(TOKENS):
with open(TOKENS, 'rb') as token:
creds = pickle.load(token)
if not (creds and creds.valid):
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
creds = flow.run_local_server()
with open(TOKENS, 'wb') as token:
pickle.dump(creds, token)
DRIVE = discovery.build('drive', 'v3', credentials=creds)
files = DRIVE.files().list().execute().get('files', [])
for f in files:
print(f['name'], f['mimeType'])
Listing your files
goo.gl/ZIgf8k
See developers.google.com/drive/api/v3/quickstart/python
13. Try our Node.js customized reporting tool codelab:
g.co/codelabs/sheets
Why use the Sheets API?
data visualization
customized reports
Sheets as a data source
Migrate SQL data to a Sheet
# read SQL data then create new spreadsheet & add rows into it
FIELDS = ('ID', 'Customer Name', 'Product Code',
'Units Ordered', 'Unit Price', 'Status')
cxn = sqlite3.connect('db.sqlite')
cur = cxn.cursor()
rows = cur.execute('SELECT * FROM orders').fetchall()
cxn.close()
rows.insert(0, FIELDS)
DATA = {'properties': {'title': 'Customer orders'}}
SHEET_ID = SHEETS.spreadsheets().create(body=DATA,
fields='spreadsheetId').execute().get('spreadsheetId')
SHEETS.spreadsheets().values().update(spreadsheetId=SHEET_ID, range='A1',
body={'values': rows}, valueInputOption='RAW').execute()
Migrate SQL data
to Sheets
goo.gl/N1RPwC
21. ● Not just for conversations
● Create microservice utilities
● Build chat bots to...
○ Automate workflows
○ Query for information
○ Other heavy-lifting
Hangouts Chat bots
(bot framework and API)
“I got to work deploying a
light-weight Flask app server…
and in less than an hour had a
working chatbot.”
Greg Meyers, Motorola Solutions CIO
goo.gl/1QeG51
Build bots for anyone, your org or your team… FAST
22. Traditional API workflow
Traditional APIs vs. Bot architecture
OAuth2
Bot architecture
"Hello World" (echo bot)
Google App Engine (Python+webapp2)
import json
import webapp2
class MainPage(webapp2.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'application/json'
event = json.loads(self.request.body)
if event['type'] == 'MESSAGE':
text = 'Hi %s. You sent: %s' % (
event['user']['displayName'], event['message']['text'])
self.response.out.write(json.dumps({'text': text}))
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
PY
Hangouts Chat
bots
goo.gl/jt3FqK
23. "Hello World" (echo bot)
Python+Flask (GAE or other hosting)
from flask import Flask, request, json
app = Flask(__name__)
@app.route('/', methods=['POST'])
def on_event():
event = request.get_json()
msg = {}
if event['type'] == 'MESSAGE':
text = 'Hi {}. You sent: {}'.format(
event['user']['displayName'], event['message']['text'])
msg = {'text': text}
return json.jsonify(msg)
PY
Hangouts Chat
bots
goo.gl/jt3FqK
05
GCP APIs
24. GCP
Build, innovate, and scale with Google Cloud Platform
BigQuery: querying Shakespeare words
TITLE = "The top 10 most common words in all of Shakespeare's works"
QUERY = '''
SELECT LOWER(word) AS word, sum(word_count) AS count
FROM [bigquery-public-data:samples.shakespeare]
GROUP BY word ORDER BY count DESC LIMIT 10
'''
rsp = BQ.query(body={'query': QUERY}, projectId=PROJ_ID).execute()
print('n*** Results for %r:n' % TITLE)
for col in rsp['schema']['fields']: # HEADERS
print(col['name'].upper(), end='t')
print()
for row in rsp['rows']: # DATA
for col in row['f']:
print(col['v'], end='t')
print()
25. Top 10 most common Shakespeare words
$ python bq_shake.py
*** Results for "The most common words in all of Shakespeare's works":
WORD COUNT
the 29801
and 27529
i 21029
to 20957
of 18514
a 15370
you 14010
my 12936
in 11722
that 11519
Simple sentiment & classification analysis
data = {'type': 'PLAIN_TEXT', 'content': '''
Google, headquartered in Mountain View, unveiled the new Android
phone at the Consumer Electronics Show. Sundar Pichai said in
his keynote that users love their new Android phones.'''
# sentiment analysis followed by content classification
sentiment = NL.documents().analyzeSentiment(
body={'document': data}).execute().get('documentSentiment')
print('TEXT:', text)
print('nSENTIMENT: score (%s), magnitude (%s)' % (
sentiment['score'], sentiment['magnitude']))
print('nCATEGORIES:')
categories = NL.documents().classifyText(
body={'document': data}).execute().get('categories')
for cat in categories:
print ('* %s (%s)' % (cat['name'][1:], cat['confidence']))
26. Simple sentiment & classification analysis
$ python nl_sent_class.py
TEXT: Google, headquartered in Mountain View, unveiled the new Android
phone at the Consumer Electronics Show. Sundar Pichai said in
his keynote that users love their new Android phones.
SENTIMENT: score (0.3), magnitude (0.6)
CATEGORIES:
* Internet & Telecom (0.76)
* Computers & Electronics (0.64)
* News (0.56)
06
Summary
27. References
● G Suite, Google Apps Script documentation & open source repos
○ developers.google.com/gsuite
○ developers.google.com/apps-script
○ github.com/gsuitedevs
● Google Cloud Platform (GCP) documentation & open source repos
○ cloud.google.com/gcp
○ cloud.google.com/docs
○ github.com/GoogleCloudPlatform/{python,nodejs}-docs-samples
○ Know AWS? Compare w/GCP at: cloud.google.com/docs/compare/aws
● Google APIs Client Libraries (G Suite & GCP) & Google Cloud SDK (GCP-only)
○ developers.google.com/api-client-library
○ cloud.google.com/sdk
More references
● Relevant videos
○ goo.gl/RbyTFD (new Google APIs project setup)
○ goo.gl/KMfbeK (common Python OAuth2 boilerplate code review)
○ goo.gl/ZIgf8k (APIs intro codelab [Drive API])
● Relevant codelabs
○ g.co/codelabs/gsuite-apis-intro (Drive API)
○ g.co/codelabs/apps-script-intro
○ codelabs.developers.google.com/codelabs/cloud-app-engine-python
○ codelabs.developers.google.com/codelabs/cloud-starting-cloudfunctions
28. Other Google APIs & platforms
● Firebase
○ firebase.google.com
● Google Data Studio (Data Visualization, dashboards, etc.)
○ marketingplatform.google.com/about/data-studio
● Actions on Google/Assistant/DialogFlow
○ developers.google.com/actions
● YouTube (Data, Analytics, and Livestreaming APIs)
○ developers.google.com/youtube
● Google Maps (Maps, Routes, and Places APIs)
○ developers.google.com/maps
Thank you! Questions?
Wesley Chun
@wescpy
Progress bars: goo.gl/69EJVw