Skip to content

Commit 1b5695f

Browse files
authored
Merge pull request #1175 from RogerSelwyn/ignore_403
Ignore 403 as well as 401
2 parents 442af95 + bb20c9a commit 1b5695f

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

O365/connection.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -914,17 +914,22 @@ def _check_delay(self) -> None:
914914
self._previous_request_at = time.time()
915915

916916
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,
918923
) -> Response:
919924
"""Internal handling of requests. Handles Exceptions.
920925
921926
:param session_obj: a requests Session instance.
922927
:param str url: url to send request to
923928
: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
925930
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.
928933
:param kwargs: extra params to send to the request api
929934
:return: Response of the request
930935
:rtype: requests.Response
@@ -983,7 +988,7 @@ def _internal_request(
983988
raise e # re-raise exception
984989
except HTTPError as e:
985990
# 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:
987992
# This could be a token expired error.
988993
if self.token_backend.token_is_expired(username=self.username):
989994
# 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:
10421047
# lazy creation of a naive session
10431048
self.naive_session = self.get_naive_session()
10441049

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+
)
10461053

10471054
def oauth_request(self, url: str, method: str, **kwargs) -> Response:
10481055
"""Makes a request to url using an oauth session.
@@ -1063,20 +1070,24 @@ def oauth_request(self, url: str, method: str, **kwargs) -> Response:
10631070
f"No auth token found. Authentication Flow needed for user {self.username}"
10641071
)
10651072

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
10711078
# raise the error.
10721079
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+
)
10741083
except TokenExpiredError as e:
10751084
# refresh and try again the request!
10761085

10771086
# try to refresh the token and/or follow token backend answer on 'should_refresh_token'
10781087
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+
)
10801091
else:
10811092
raise e
10821093

0 commit comments

Comments
 (0)