X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=gdata_oauth.py;fp=gdata_oauth.py;h=19fa98b207a58d173de1dec740b96de18d8ceb77;hb=c06bfef53f70551e7920bc4facce27f47b89e2ba;hp=1f9cd67b1e59e9188f4f9486c4923493e5b84a8b;hpb=6cc940e0df9b8ea937fb955f959fa878c80f0d7c;p=kiosk.git diff --git a/gdata_oauth.py b/gdata_oauth.py index 1f9cd67..19fa98b 100644 --- a/gdata_oauth.py +++ b/gdata_oauth.py @@ -1,3 +1,5 @@ +#!/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 @@ -25,7 +27,7 @@ import ssl 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 @@ -55,12 +57,12 @@ class OAuth: # 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) @@ -68,19 +70,19 @@ class OAuth: 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", @@ -97,12 +99,12 @@ class OAuth: 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") @@ -135,7 +137,7 @@ class OAuth: 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 @@ -172,7 +174,7 @@ class OAuth: 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