@@ -581,14 +581,6 @@ def __init__(
581
581
"https://login.microsoftonline.com/common/oauth2/nativeclient"
582
582
)
583
583
584
- # In the event of a response that returned 401 unauthorised this will flag between requests
585
- # that this 401 can be a token expired error. MsGraph is returning 401 when the access token
586
- # has expired. We can not distinguish between a real 401 or token expired 401. So in the event
587
- # of a 401 http error we will first try to refresh the token, set this flag to True and then
588
- # re-run the request. If the 401 goes away we will then set this flag to false. If it keeps the
589
- # 401 then we will raise the error.
590
- #: Indicates if the token has expired. |br| **Type:** bool
591
- self ._token_expired_flag : bool = False
592
584
593
585
@property
594
586
def auth_flow_type (self ) -> str :
@@ -978,13 +970,17 @@ def _check_delay(self) -> None:
978
970
self ._previous_request_at = time .time ()
979
971
980
972
def _internal_request (
981
- self , session_obj : Session , url : str , method : str , ** kwargs
973
+ self , session_obj : Session , url : str , method : str , ignore401 : bool = False , ** kwargs
982
974
) -> Response :
983
975
"""Internal handling of requests. Handles Exceptions.
984
976
985
977
:param session_obj: a requests Session instance.
986
978
:param str url: url to send request to
987
979
:param str method: type of request (get/put/post/patch/delete)
980
+ :param bool ignore401: indicates whether to ignore 401 error when it would
981
+ indicate that there the token has expired. This is set to 'True' for the
982
+ first call to the api, and 'False' for the call that is initiated after a
983
+ tpken refresh.
988
984
:param kwargs: extra params to send to the request api
989
985
:return: Response of the request
990
986
:rtype: requests.Response
@@ -1043,14 +1039,13 @@ def _internal_request(
1043
1039
raise e # re-raise exception
1044
1040
except HTTPError as e :
1045
1041
# Server response with 4XX or 5XX error status codes
1046
- if e .response .status_code == 401 and self . _token_expired_flag is False :
1042
+ if e .response .status_code == 401 and ignore401 is True :
1047
1043
# This could be a token expired error.
1048
1044
if self .token_backend .token_is_expired (username = self .username ):
1049
1045
# Access token has expired, try to refresh the token and try again on the next loop
1050
1046
# By raising custom exception TokenExpiredError we signal oauth_request to fire a
1051
1047
# refresh token operation.
1052
1048
log .debug (f"Oauth Token is expired for username: { self .username } " )
1053
- self ._token_expired_flag = True
1054
1049
raise TokenExpiredError ("Oauth Token is expired" )
1055
1050
1056
1051
# try to extract the error message:
@@ -1103,7 +1098,7 @@ def naive_request(self, url: str, method: str, **kwargs) -> Response:
1103
1098
# lazy creation of a naive session
1104
1099
self .naive_session = self .get_naive_session ()
1105
1100
1106
- return self ._internal_request (self .naive_session , url , method , ** kwargs )
1101
+ return self ._internal_request (self .naive_session , url , method , ignore401 = False , ** kwargs )
1107
1102
1108
1103
def oauth_request (self , url : str , method : str , ** kwargs ) -> Response :
1109
1104
"""Makes a request to url using an oauth session.
@@ -1124,18 +1119,22 @@ def oauth_request(self, url: str, method: str, **kwargs) -> Response:
1124
1119
f"No auth token found. Authentication Flow needed for user { self .username } "
1125
1120
)
1126
1121
1122
+ # In the event of a response that returned 401 unauthorised the ignore401 flag indicates
1123
+ # that the 401 can be a token expired error. MsGraph is returning 401 when the access token
1124
+ # has expired. We can not distinguish between a real 401 or token expired 401. So in the event
1125
+ # of a 401 http error we will ignore the first time and try to refresh the token, and then
1126
+ # re-run the request. If the 401 goes away we can move on. If it keeps the 401 then we will
1127
+ # raise the error.
1127
1128
try :
1128
- return self ._internal_request (self .session , url , method , ** kwargs )
1129
+ return self ._internal_request (self .session , url , method , ignore401 = True , ** kwargs )
1129
1130
except TokenExpiredError as e :
1130
1131
# refresh and try again the request!
1131
- try :
1132
- # try to refresh the token and/or follow token backend answer on 'should_refresh_token'
1133
- if self ._try_refresh_token ():
1134
- return self ._internal_request (self .session , url , method , ** kwargs )
1135
- else :
1136
- raise e
1137
- finally :
1138
- self ._token_expired_flag = False
1132
+
1133
+ # try to refresh the token and/or follow token backend answer on 'should_refresh_token'
1134
+ if self ._try_refresh_token ():
1135
+ return self ._internal_request (self .session , url , method , ignore401 = False , ** kwargs )
1136
+ else :
1137
+ raise e
1139
1138
1140
1139
def get (self , url : str , params : Optional [dict ] = None , ** kwargs ) -> Response :
1141
1140
"""Shorthand for self.oauth_request(url, 'get')
0 commit comments