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