|
| 1 | +# SPDX-FileCopyrightText: 2021 Brent Rubell, written for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | +import ssl |
| 5 | +import board |
| 6 | +import wifi |
| 7 | +import socketpool |
| 8 | +import adafruit_requests as requests |
| 9 | +from adafruit_oauth2 import OAuth2 |
| 10 | +from adafruit_display_text.label import Label |
| 11 | +from adafruit_bitmap_font import bitmap_font |
| 12 | +from adafruit_magtag.magtag import Graphics |
| 13 | +from adafruit_display_shapes.rect import Rect |
| 14 | + |
| 15 | +# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
| 16 | +# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
| 17 | +# source control. |
| 18 | +# pylint: disable=no-name-in-module,wrong-import-order |
| 19 | +try: |
| 20 | + from secrets import secrets |
| 21 | +except ImportError: |
| 22 | + print("Credentials and tokens are kept in secrets.py, please add them there!") |
| 23 | + raise |
| 24 | + |
| 25 | +print("Connecting to %s" % secrets["ssid"]) |
| 26 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 27 | +print("Connected to %s!" % secrets["ssid"]) |
| 28 | + |
| 29 | +pool = socketpool.SocketPool(wifi.radio) |
| 30 | +requests = requests.Session(pool, ssl.create_default_context()) |
| 31 | + |
| 32 | +# DisplayIO setup |
| 33 | +font_small = bitmap_font.load_font("/fonts/Arial-12.pcf") |
| 34 | +font_large = bitmap_font.load_font("/fonts/Arial-14.pcf") |
| 35 | + |
| 36 | +graphics = Graphics(auto_refresh=False) |
| 37 | +display = graphics.display |
| 38 | + |
| 39 | +background = Rect(0, 0, 296, 128, fill=0xFFFFFF) |
| 40 | +graphics.splash.append(background) |
| 41 | + |
| 42 | +label_overview_text = Label( |
| 43 | + font_large, |
| 44 | + x=0, |
| 45 | + y=10, |
| 46 | + line_spacing=0.75, |
| 47 | + color=0x000000, |
| 48 | + text="Authorize this device with Google:", |
| 49 | +) |
| 50 | +graphics.splash.append(label_overview_text) |
| 51 | + |
| 52 | +label_verification_url = Label( |
| 53 | + font_small, x=0, y=40, line_spacing=0.75, color=0x000000, max_glyphs=90 |
| 54 | +) |
| 55 | +graphics.splash.append(label_verification_url) |
| 56 | + |
| 57 | +label_user_code = Label( |
| 58 | + font_small, x=0, y=80, color=0x000000, line_spacing=0.75, max_glyphs=50 |
| 59 | +) |
| 60 | +graphics.splash.append(label_user_code) |
| 61 | + |
| 62 | +label_qr_code = Label( |
| 63 | + font_small, x=0, y=100, color=0x000000, text="Or scan the QR code:" |
| 64 | +) |
| 65 | +graphics.splash.append(label_qr_code) |
| 66 | + |
| 67 | +# Set scope(s) of access required by the API you're using |
| 68 | +scopes = ["https://www.googleapis.com/auth/calendar.readonly"] |
| 69 | + |
| 70 | +# Initialize an OAuth2 object |
| 71 | +google_auth = OAuth2( |
| 72 | + requests, secrets["google_client_id"], secrets["google_client_secret"], scopes |
| 73 | +) |
| 74 | + |
| 75 | +# Request device and user codes |
| 76 | +# https://developers.google.com/identity/protocols/oauth2/limited-input-device#step-1:-request-device-and-user-codes |
| 77 | +google_auth.request_codes() |
| 78 | + |
| 79 | +# Display user code and verification url |
| 80 | +# NOTE: If you are displaying this on a screen, ensure the text label fields are |
| 81 | +# long enough to handle the user_code and verification_url. |
| 82 | +# Details in link below: |
| 83 | +# https://developers.google.com/identity/protocols/oauth2/limited-input-device#displayingthecode |
| 84 | +print( |
| 85 | + "1) Navigate to the following URL in a web browser:", google_auth.verification_url |
| 86 | +) |
| 87 | +print("2) Enter the following code:", google_auth.user_code) |
| 88 | +label_verification_url.text = ( |
| 89 | + "1. On your computer or mobile device,\ngo to %s" % google_auth.verification_url |
| 90 | +) |
| 91 | +label_user_code.text = "2. Enter code: %s" % google_auth.user_code |
| 92 | + |
| 93 | +graphics.qrcode(google_auth.verification_url.encode(), qr_size=2, x=240, y=70) |
| 94 | +board.DISPLAY.show(graphics.splash) |
| 95 | +display.refresh() |
| 96 | + |
| 97 | +# Poll Google's authorization server |
| 98 | +print("Waiting for browser authorization...") |
| 99 | +if not google_auth.wait_for_authorization(): |
| 100 | + raise RuntimeError("Timed out waiting for browser response!") |
| 101 | + |
| 102 | +print("Successfully Authenticated with Google!") |
| 103 | +print("Add the following lines to your secrets.py file:") |
| 104 | +print("\t'google_access_token' " + ":" + " '%s'," % google_auth.access_token) |
| 105 | +print("\t'google_refresh_token' " + ":" + " '%s'" % google_auth.refresh_token) |
| 106 | + |
| 107 | +graphics.splash.pop() |
| 108 | +graphics.splash.pop() |
| 109 | +graphics.splash.pop() |
| 110 | + |
| 111 | +label_overview_text.text = "Successfully Authenticated!" |
| 112 | +label_verification_url.text = ( |
| 113 | + "Check the REPL for tokens to add\n\tto your secrets.py file" |
| 114 | +) |
| 115 | +display.refresh() |
0 commit comments