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