refine Chat calls

This commit is contained in:
Jay Lee
2021-05-25 09:32:02 -04:00
parent 0e7472de50
commit c4401f8bd4
2 changed files with 23 additions and 10 deletions

View File

@ -843,13 +843,14 @@ def getSvcAcctCredentials(scopes, act_as, api=None):
else: else:
audience = f'https://{api}.googleapis.com/' audience = f'https://{api}.googleapis.com/'
if sign_method == 'default': if sign_method == 'default':
return JWTCredentials.from_service_account_info(GM_Globals[GM_OAUTH2SERVICE_JSON_DATA], credentials = JWTCredentials.from_service_account_info(GM_Globals[GM_OAUTH2SERVICE_JSON_DATA],
audience=audience) audience=audience)
elif sign_method == 'yubikey': elif sign_method == 'yubikey':
yksigner = yubikey.YubiKey(GM_Globals[GM_OAUTH2SERVICE_JSON_DATA]) yksigner = yubikey.YubiKey(GM_Globals[GM_OAUTH2SERVICE_JSON_DATA])
credentials = JWTCredentials._from_signer_and_info(yksigner, credentials = JWTCredentials._from_signer_and_info(yksigner,
GM_Globals[GM_OAUTH2SERVICE_JSON_DATA], GM_Globals[GM_OAUTH2SERVICE_JSON_DATA],
audience=audience) audience=audience)
credentials.project_id = GM_Globals[GM_OAUTH2SERVICE_JSON_DATA]['project_id']
GM_Globals[GM_OAUTH2SERVICE_ACCOUNT_CLIENT_ID] = GM_Globals[ GM_Globals[GM_OAUTH2SERVICE_ACCOUNT_CLIENT_ID] = GM_Globals[
GM_OAUTH2SERVICE_JSON_DATA]['client_id'] GM_OAUTH2SERVICE_JSON_DATA]['client_id']
return credentials return credentials

View File

@ -23,7 +23,6 @@ def build():
THROW_REASONS = [ THROW_REASONS = [
gapi_errors.ErrorReason.FOUR_O_FOUR, # Chat API not configured gapi_errors.ErrorReason.FOUR_O_FOUR, # Chat API not configured
gapi_errors.ErrorReason.FOUR_O_THREE, # Bot not added to room
] ]
def _chat_error_handler(chat, err): def _chat_error_handler(chat, err):
@ -32,16 +31,25 @@ def _chat_error_handler(chat, err):
url = f'https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat?project={project_id}' url = f'https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat?project={project_id}'
print('ERROR: you need to configure Google Chat for your API project. Please go to:') print('ERROR: you need to configure Google Chat for your API project. Please go to:')
print() print()
print(url) print(f' {url}')
print() print()
print('and complete all fields.') print('and complete all fields.')
elif err.status_code == 403: else:
print('ERROR: no access to that Chat space or message. Make sure your bot created the mesage and the user has chatted the bot first or added it to the Chat room') raise err
sys.exit(1) sys.exit(1)
def print_spaces(): def print_spaces():
chat = build() chat = build()
todrive = False
i =3
while i < len(sys.argv):
myarg = sys.argv[i].lower()
if myarg == 'todrive':
todrive = True
i += 1
else:
controlflow.invalid_argument_exit(myarg, 'gam print chatspaces')
try: try:
spaces = gapi.get_all_pages(chat.spaces(), 'list', 'spaces', throw_reasons=THROW_REASONS) spaces = gapi.get_all_pages(chat.spaces(), 'list', 'spaces', throw_reasons=THROW_REASONS)
except googleapiclient.errors.HttpError as err: except googleapiclient.errors.HttpError as err:
@ -49,12 +57,13 @@ def print_spaces():
if not spaces: if not spaces:
print('Bot not added to any Chat rooms or users yet.') print('Bot not added to any Chat rooms or users yet.')
else: else:
display.write_csv_file(spaces, spaces[0].keys(), 'Chat Spaces', False) display.write_csv_file(spaces, spaces[0].keys(), 'Chat Spaces', todrive)
def print_members(): def print_members():
chat = build() chat = build()
space = None space = None
todrive = False
i = 3 i = 3
while i < len(sys.argv): while i < len(sys.argv):
myarg = sys.argv[i].lower() myarg = sys.argv[i].lower()
@ -63,6 +72,9 @@ def print_members():
if space[:7] != 'spaces/': if space[:7] != 'spaces/':
space = f'spaces/{space}' space = f'spaces/{space}'
i += 2 i += 2
elif myarg == 'todrive':
todrive = True
i += 1
else: else:
controlflow.invalid_argument_exit(myarg, "gam print chatmembers") controlflow.invalid_argument_exit(myarg, "gam print chatmembers")
try: try:
@ -77,7 +89,7 @@ def print_members():
if key not in titles: if key not in titles:
titles.append(key) titles.append(key)
members.append(utils.flatten_json(result)) members.append(utils.flatten_json(result))
display.write_csv_file(members, titles, 'Chat Members', False) display.write_csv_file(members, titles, 'Chat Members', todrive)
def create_message(): def create_message():