exploring google apis with python
TRANSCRIPT
Exploring Google APIswith PythonPyConMay 2016: Portland, OR
+Wesley Chun, @wescpyDeveloper Advocate
BayPIGgiesAug 2016: Silicon Valley, CA
I teach
I write
I code
Outline
●Overview●Getting started●Python● Live demo(?)●Wrap-up
Part I: Google APIs overview
BUT... wait, there’s more...
Part II: Getting Started with Google APIs
Developers Console (devconsole)
OAuth2 scenarios● Varies on application type (where apps located)○ Web (server) applications○ Installed (mobile & desktop computers) apps○ Service accounts (cloud/server-to-server)○ Client-side (JavaScript) apps○ Limited-input devices (game consoles, printers, cameras, etc.)
○ developers.google.com/identity/protocols/OAuth2○ TODAY: command-line script == "Installed"
Part III: Using Google APIs with Python
developers.google.com/discovery/libraries
SIMPLE
AUTHORIZED
API access
Simple API access
General OAuth2 process● Goal: valid access token == 3rd-party API data access
● How OAuth2 works (in general)○ Step 1: Create OAuth2 credentials in DevConsole○ Step 2: Send credentials to get access & refresh tokens○ Step 3: Use access token to make authorized API calls○ Step 4: Access tokens expire; use refresh to get new one
● developers.google.com/identity/protocols/OAuth2● Others will be similar (with slight differences)
Authorized API access
SIMPLE
AUTHORIZED
Which do you choose?
Part IV: Demo (live would make it interesting)
Searching Google+ for social posts
Searching YouTube for videos
Listing your files in Google Drive
Searching Gmail for threads
Reading cells from a Google Sheetdocs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/editUniversity student activity Sheet from developers.google.com/sheets/quickstart/python
Creating events in Google Calendar
Use of multiple APIs supportedSimple, authorized, or a mix of both types of APIs; re-uses HTTP client; also see goo.gl/cdm3kZ
Other examples● Google Drive API: upload/download○ Actually import (txt as Google Docs)/export (as PDF)
● Google Sheets API: create & read○ Migrating SQL data to a Google Sheet
● Google _________(which of the pair above?) API:○ Export Google Sheet as csv
● Gmail API: change email signature (coming soon)○ With new API settings endpoints (aliases, forwarding, vacation
responder, filters, etc.)● Google Calendar API: modify events (coming soon)○ Many ways of creating events; here's how to modify them
● Most examples: wescpy.blogspot.com else developers.google.com
Write your own or see https://github.com/googledrive/zipextractor (JS)
Part V: Wrap-up
Reference● Google Developers Console○ console.developers.google.com
● Google APIs Client Library for Python (2 & 3)○ pip{,3} install -U google-api-python-client○ developers.google.com/api-client-library/python■ .../python/guide/{django,google_app_engine} # special
● Blogpost resources○ goo.gl/57Gufk (simple API access)○ goo.gl/cdm3kZ (authorized API access; Google Drive API)○ goo.gl/OfCbOz (Gmail API)○ goo.gl/KuYMiq (Google Calendar API)○ goo.gl/A3kb6o and goo.gl/nxuR9w (Google Drive API v2 & v3)○ goo.gl/CtIhx4 (Google Sheets API)
Reference● Video Resources○ SETUP & BOILERPLATE■ goo.gl/RbyTFD (new Google APIs project setup)■ goo.gl/KMfbeK (common Python boilerplate code review)
○ GOOGLE APIs■ goo.gl/ZIgf8k and goo.gl/EySSQV (Google Drive API)■ goo.gl/pFYUQ2 (Gmail API)■ goo.gl/9qKHVA (Google Calendar API)■ goo.gl/N1RPwC (Google Sheets API)
Alternative 1● Google Apps Script○ What is it?■ Executes as JavaScript in Google's cloud■ Think “built-in” services vs. traditional API usage■ Convenient access to Google Apps data■ Can also access other Google+external services
○ Video resources■ goo.gl/1sXeuD (Apps Script intro)■ goo.gl/oAzBN9 (Google Maps & Gmail APIs)■ goo.gl/YuwZ0E (YouTube [Data] API)■ goo.gl/xSzGl4 (Google Forms)
Alternative 2
● What about my favorite HTTP request library?○ requests■ python-requests.org
○ So can I use 'requests' to talk to Google APIs?○ Answer: YES, but...■ Not many examples out there■ I found one at gist.github.com/ymotongpoo/1907281■ BUT look at it, ~80% of code is auth{entic,oriz}ation■ Client Library hides most of it from you■ Summary: yes you can, but it'll be a DIY effort
Alternatives for other Google APIs● Most, not all, Google APIs work with standard Client Library
● Other APIs/platforms have their own○ Google AdWords■ developers.google.com/adwords/api/docs/clientlibraries
○ Google Maps (Web Services)■ developers.google.com/maps/web-services/client-library
○ Google Cloud Platform■ cloud.google.com/python/apis
○ Firebase platform (Android, iOS, Web, C++, Node.js, Java)■ firebase.google.com/docs/libraries
Summary
● We’ve created great tech but can’t build everything(!)● APIs: our tech available for your apps; free?!?● Access w/favorite dev tool via Google APIs Client Library○ Python, JavaScript (client + Node.js), Java, C#/.NET, Go,
Dart, Ruby, PHP○ Also mobile: Android (Java), iOS (Swift, Objective-C)
● Build the next great thing!
Thank you! Questions?
+Wesley Chungoogle.com/+WesleyChun
@wescpytwitter.com/wescpy
slide deckgoo.gl/d1awLK