@@ -914,17 +914,22 @@ def _check_delay(self) -> None:
914
914
self ._previous_request_at = time .time ()
915
915
916
916
def _internal_request (
917
- self , session_obj : Session , url : str , method : str , ignore401 : bool = False , ** kwargs
917
+ self ,
918
+ session_obj : Session ,
919
+ url : str ,
920
+ method : str ,
921
+ ignore40x : bool = False ,
922
+ ** kwargs ,
918
923
) -> Response :
919
924
"""Internal handling of requests. Handles Exceptions.
920
925
921
926
:param session_obj: a requests Session instance.
922
927
:param str url: url to send request to
923
928
:param str method: type of request (get/put/post/patch/delete)
924
- :param bool ignore401 : indicates whether to ignore 401 error when it would
929
+ :param bool ignore40x : indicates whether to ignore 40x errors when it would
925
930
indicate that there the token has expired. This is set to 'True' for the
926
- first call to the api, and 'False' for the call that is initiated after a
927
- tpken refresh.
931
+ first call to the api, and 'False' for the call that is initiated after a
932
+ tpken refresh.
928
933
:param kwargs: extra params to send to the request api
929
934
:return: Response of the request
930
935
:rtype: requests.Response
@@ -983,7 +988,7 @@ def _internal_request(
983
988
raise e # re-raise exception
984
989
except HTTPError as e :
985
990
# Server response with 4XX or 5XX error status codes
986
- if e .response .status_code == 401 and ignore401 is True :
991
+ if e .response .status_code in [ 401 , 403 ] and ignore40x is True :
987
992
# This could be a token expired error.
988
993
if self .token_backend .token_is_expired (username = self .username ):
989
994
# Access token has expired, try to refresh the token and try again on the next loop
@@ -1042,7 +1047,9 @@ def naive_request(self, url: str, method: str, **kwargs) -> Response:
1042
1047
# lazy creation of a naive session
1043
1048
self .naive_session = self .get_naive_session ()
1044
1049
1045
- return self ._internal_request (self .naive_session , url , method , ignore401 = False , ** kwargs )
1050
+ return self ._internal_request (
1051
+ self .naive_session , url , method , ignore40x = False , ** kwargs
1052
+ )
1046
1053
1047
1054
def oauth_request (self , url : str , method : str , ** kwargs ) -> Response :
1048
1055
"""Makes a request to url using an oauth session.
@@ -1063,20 +1070,24 @@ def oauth_request(self, url: str, method: str, **kwargs) -> Response:
1063
1070
f"No auth token found. Authentication Flow needed for user { self .username } "
1064
1071
)
1065
1072
1066
- # In the event of a response that returned 401 unauthorised the ignore401 flag indicates
1067
- # that the 401 can be a token expired error. MsGraph is returning 401 when the access token
1068
- # has expired. We can not distinguish between a real 401 or token expired 401 . So in the event
1069
- # of a 401 http error we will ignore the first time and try to refresh the token, and then
1070
- # re-run the request. If the 401 goes away we can move on. If it keeps the 401 then we will
1073
+ # In the event of a response that returned 401 or 403 unauthorised the ignore40x flag indicates
1074
+ # that the 40x can be a token expired error. MsGraph is returning 401 or 403 when the access token
1075
+ # has expired. We can not distinguish between a real 40x or token expired 40x . So in the event
1076
+ # of a 40x http error we will ignore the first time and try to refresh the token, and then
1077
+ # re-run the request. If the 40x goes away we can move on. If it keeps the 40x then we will
1071
1078
# raise the error.
1072
1079
try :
1073
- return self ._internal_request (self .session , url , method , ignore401 = True , ** kwargs )
1080
+ return self ._internal_request (
1081
+ self .session , url , method , ignore40x = True , ** kwargs
1082
+ )
1074
1083
except TokenExpiredError as e :
1075
1084
# refresh and try again the request!
1076
1085
1077
1086
# try to refresh the token and/or follow token backend answer on 'should_refresh_token'
1078
1087
if self ._try_refresh_token ():
1079
- return self ._internal_request (self .session , url , method , ignore401 = False , ** kwargs )
1088
+ return self ._internal_request (
1089
+ self .session , url , method , ignore40x = False , ** kwargs
1090
+ )
1080
1091
else :
1081
1092
raise e
1082
1093
0 commit comments