X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=gdata_oauth.py;fp=gdata_oauth.py;h=7e8b336db29380387b11808c0cecf0d352db4455;hb=e4dca16bbd329afdb587e8488767d88e17777254;hp=43223e99bdc5912f17e92c244fa256bba188d2b2;hpb=ba913d3c5ec6fd5e229398ebfe9e073aaae7d73c;p=kiosk.git diff --git a/gdata_oauth.py b/gdata_oauth.py index 43223e9..7e8b336 100644 --- a/gdata_oauth.py +++ b/gdata_oauth.py @@ -15,12 +15,13 @@ except ImportError: import os.path import json import time -from oauth2client.client import OAuth2Credentials -import gdata.calendar.service -import gdata.docs.service -import gdata.photos.service, gdata.photos -from googleapiclient.discovery import build -import httplib2 +from typing import Dict, Optional +from oauth2client.client import OAuth2Credentials # type: ignore +import gdata.calendar.service # type: ignore +import gdata.docs.service # type: ignore +import gdata.photos.service, gdata.photos # type: ignore +from googleapiclient.discovery import build # type: ignore +import httplib2 # type: ignore from googleapiclient.discovery import build import datetime import ssl @@ -31,10 +32,10 @@ class OAuth: print("gdata: initializing oauth token...") self.client_id = client_id self.client_secret = client_secret - self.user_code = None + self.user_code: Optional[str] = None # print 'Client id: %s' % (client_id) # print 'Client secret: %s' % (client_secret) - self.token = None + self.token: Optional[Dict] = None self.device_code = None self.verfication_url = None self.token_file = "client_secrets.json" @@ -51,8 +52,8 @@ class OAuth: self.host = "accounts.google.com" self.reset_connection() self.load_token() - self.last_action = 0 - self.ssl_ctx = None + self.last_action = 0.0 + self.ssl_ctx: Optional[ssl.SSLContext] = None # this setup is isolated because it eventually generates a BadStatusLine # exception, after which we always get httplib.CannotSendRequest errors. @@ -82,7 +83,7 @@ class OAuth: print("gdata: we have no token.") return self.token is not None - def get_user_code(self) -> str: + def get_user_code(self) -> Optional[str]: self.conn.request( "POST", "/o/oauth2/device/code", @@ -99,6 +100,7 @@ class OAuth: self.verification_url = data["verification_url"] self.retry_interval = data["interval"] else: + self.user_code = None print(f"gdata: {response.status}") print(response.read()) sys.exit(-1) @@ -144,6 +146,9 @@ class OAuth: else: print("gdata: trying to refresh oauth token...") self.reset_connection() + if self.token is None: + return False + refresh_token = self.token["refresh_token"] self.conn.request( "POST", @@ -162,7 +167,7 @@ class OAuth: response = self.conn.getresponse() self.last_action = time.time() if response.status == 200: - data = json.loads(response.read()) + data: Dict = json.loads(response.read()) if "access_token" in data: self.token = data # in fact we NEVER get a new refresh token at this point