diff --git a/src/auth/oauth.py b/src/auth/oauth.py index ba186756..72faebc9 100644 --- a/src/auth/oauth.py +++ b/src/auth/oauth.py @@ -17,6 +17,7 @@ import transport from var import GAM_INFO from var import GM_Globals from var import GM_WINDOWS +import utils MESSAGE_CONSOLE_AUTHORIZATION_PROMPT = ('\nGo to the following link in your ' 'browser:\n\n\t{url}\n') @@ -512,29 +513,8 @@ class _ShortURLFlow(google_auth_oauthlib.flow.InstalledAppFlow): def authorization_url(self, http=None, **kwargs): """Gets a shortened authorization URL.""" long_url, state = super(_ShortURLFlow, self).authorization_url(**kwargs) - if not http: - http = transport.create_http(timeout=10) - headers = {'Content-Type': 'application/json', 'User-Agent': GAM_INFO} - try: - payload = json.dumps({'long_url': long_url}) - resp, content = http.request( - _ShortURLFlow.URL_SHORTENER_ENDPOINT, - 'POST', - payload, - headers=headers) - except: - return long_url, state - - if resp.status != 200: - return long_url, state - - try: - if isinstance(content, bytes): - content = content.decode() - return json.loads(content).get('short_url', long_url), state - except: - return long_url, state - + short_url = utils.shorten_url(long_url) + return short_url, state class _FileLikeThreadLock(object): """A threading.lock which has the same interface as filelock.Filelock.""" diff --git a/src/gam.py b/src/gam.py index e0a9aa6c..f97434f1 100755 --- a/src/gam.py +++ b/src/gam.py @@ -1021,7 +1021,7 @@ def doCheckServiceAccount(users): long_url = (f'https://admin.google.com/{user_domain}/ManageOauthClients' f'?clientScopeToAdd={",".join(check_scopes)}' f'&clientNameToAdd={service_account}') - short_url = shorten_url(long_url) + short_url = utils.shorten_url(long_url) scopes_failed = f'''Some scopes failed! To authorize them, please go to: {short_url} @@ -10104,7 +10104,7 @@ def doRequestOAuth(login_hint=None): try: creds = auth.oauth.Credentials.from_client_secrets_file( client_secrets_file=client_secrets_file, - scopes=list(scopes), + scopes=scopes, access_type='offline', login_hint=login_hint, credentials_file=GC_Values[GC_OAUTH2_TXT], @@ -10421,7 +10421,7 @@ class ScopeSelectionMenu(): """Returns the aggregate set of oauth scopes currently selected.""" selected_scopes = [scope for option in self.get_selected_options() for scope in option.get_effective_scopes()] - return set(selected_scopes) + return list(set(selected_scopes)) MENU_CHOICE = { 'SELECT_ALL_SCOPES': 's', diff --git a/src/utils.py b/src/utils.py index bf86529f..cd245258 100644 --- a/src/utils.py +++ b/src/utils.py @@ -4,10 +4,11 @@ import sys from hashlib import md5 from html.entities import name2codepoint from html.parser import HTMLParser +import json from var import * import fileutils - +import transport class _DeHTMLParser(HTMLParser): @@ -252,3 +253,27 @@ def md5_matches_file(local_file, expected_md5, exitOnError): if exitOnError and actual_hash != expected_md5: controlflow.system_error_exit(6, f'actual hash was {actual_hash}. Exiting on corrupt file.') return actual_hash == expected_md5 + +URL_SHORTENER_ENDPOINT = 'https://gam-shortn.appspot.com/create' + +def shorten_url(long_url, httpc=None): + if not httpc: + httpc = transport.create_http(timeout=10) + headers = {'Content-Type': 'application/json', 'User-Agent': GAM_INFO} + try: + payload = json.dumps({'long_url': long_url}) + resp, content = httpc.request( + URL_SHORTENER_ENDPOINT, + 'POST', + payload, + headers=headers) + except: + return long_url + if resp.status != 200: + return long_url + try: + if isinstance(content, bytes): + content = content.decode() + return json.loads(content).get('short_url', long_url) + except: + return long_url diff --git a/src/var.py b/src/var.py index 494c862e..4037465e 100644 --- a/src/var.py +++ b/src/var.py @@ -6,7 +6,7 @@ import platform import re gam_author = 'Jay Lee ' -gam_version = '5.01' +gam_version = '5.02' gam_license = 'Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)' GAM_URL = 'https://git.io/gam'