From 5e073e0c68fcbdc558133bdbd59a02453e597abe Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Thu, 25 Nov 2021 17:15:49 +0300 Subject: [PATCH] get_access_token: error checking In case if msal_app().acquire_token_silent() returns an error raise an exception with error details instead of crashing with KeyError --- .../connector/auth/microsoft_app_credentials.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py b/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py index d625d6ede..cfe6c18e2 100644 --- a/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py +++ b/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py @@ -62,6 +62,14 @@ def get_access_token(self, force_refresh: bool = False) -> str: auth_token = self.__get_msal_app().acquire_token_for_client( scopes=self.scopes ) + + # acquire_token...() returns None if there are no any tokens and + # a dict which contains 'error' in case of error + if auth_token is None: + raise UserWarning("No any access tokens") + if 'error' in auth_token: + raise UserWarning(f"Failed to get access token: {auth_token}") + return auth_token["access_token"] def __get_msal_app(self):