+#!/usr/bin/env python3
+
# https://developers.google.com/accounts/docs/OAuth2ForDevices
# https://developers.google.com/drive/web/auth/web-server
# https://developers.google.com/google-apps/calendar/v3/reference/calendars
class OAuth:
- def __init__(self, client_id, client_secret):
+ def __init__(self, client_id: str, client_secret: str) -> None:
print("gdata: initializing oauth token...")
self.client_id = client_id
self.client_secret = client_secret
# this setup is isolated because it eventually generates a BadStatusLine
# exception, after which we always get httplib.CannotSendRequest errors.
# When this happens, we try re-creating the exception.
- def reset_connection(self):
+ def reset_connection(self) -> None:
self.ssl_ctx = ssl.create_default_context(cafile="/usr/local/etc/ssl/cert.pem")
http.client.HTTPConnection.debuglevel = 2
self.conn = http.client.HTTPSConnection(self.host, context=self.ssl_ctx)
- def load_token(self):
+ def load_token(self) -> None:
token = None
if os.path.isfile(self.token_file):
f = open(self.token_file)
self.token = json.loads(json_token)
f.close()
- def save_token(self):
+ def save_token(self) -> None:
f = open(self.token_file, "w")
f.write(json.dumps(self.token))
f.close()
- def has_token(self):
+ def has_token(self) -> bool:
if self.token != None:
print("gdata: we have a token!")
else:
print("gdata: we have no token.")
return self.token != None
- def get_user_code(self):
+ def get_user_code(self) -> str:
self.conn.request(
"POST",
"/o/oauth2/device/code",
self.verification_url = data["verification_url"]
self.retry_interval = data["interval"]
else:
- print(("gdata: %d" % response.status))
- print((response.read()))
- sys.exit()
+ print(f"gdata: {response.status}")
+ print(response.read())
+ sys.exit(-1)
return self.user_code
- def get_new_token(self):
+ def get_new_token(self) -> None:
# call get_device_code if not already set
if self.user_code == None:
print("gdata: getting user code")
print((response.status))
print((response.read()))
- def refresh_token(self):
+ def refresh_token(self) -> bool:
if self.checking_too_often():
print("gdata: not refreshing yet, too soon...")
return False
print((response.read()))
return False
- def checking_too_often(self):
+ def checking_too_often(self) -> bool:
now = time.time()
return (now - self.last_action) <= 30