exploring google apis with python

27
Exploring Google APIs with Python PyCon May 2016: Portland, OR +Wesley Chun, @wescpy Developer Advocate BayPIGgies Aug 2016: Silicon Valley, CA I teach

Upload: wesley-chun

Post on 13-Apr-2017

563 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Exploring Google APIs with Python

Exploring Google APIswith PythonPyConMay 2016: Portland, OR

+Wesley Chun, @wescpyDeveloper Advocate

BayPIGgiesAug 2016: Silicon Valley, CA

I teach

Page 2: Exploring Google APIs with Python

I write

I code

Page 3: Exploring Google APIs with Python

Outline

●Overview●Getting started●Python● Live demo(?)●Wrap-up

Part I: Google APIs overview

Page 4: Exploring Google APIs with Python

BUT... wait, there’s more...

Page 5: Exploring Google APIs with Python
Page 6: Exploring Google APIs with Python

Part II: Getting Started with Google APIs

Page 7: Exploring Google APIs with Python

Developers Console (devconsole)

Page 8: Exploring Google APIs with Python
Page 9: Exploring Google APIs with Python
Page 10: Exploring Google APIs with Python
Page 11: Exploring Google APIs with Python

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"

Page 12: Exploring Google APIs with Python
Page 13: Exploring Google APIs with Python
Page 14: Exploring Google APIs with Python

Part III: Using Google APIs with Python

Page 15: Exploring Google APIs with Python

developers.google.com/discovery/libraries

SIMPLE

AUTHORIZED

API access

Page 16: Exploring Google APIs with Python

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)

Page 17: Exploring Google APIs with Python

Authorized API access

SIMPLE

AUTHORIZED

Which do you choose?

Page 18: Exploring Google APIs with Python

Part IV: Demo (live would make it interesting)

Searching Google+ for social posts

Page 19: Exploring Google APIs with Python

Searching YouTube for videos

Listing your files in Google Drive

Page 20: Exploring Google APIs with Python

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

Page 21: Exploring Google APIs with 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

Page 22: Exploring Google APIs with Python

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)

Page 23: Exploring Google APIs with Python
Page 24: Exploring Google APIs with Python

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)

Page 25: Exploring Google APIs with Python

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)

Page 26: Exploring Google APIs with Python

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

Page 27: Exploring Google APIs with Python

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