From fc7cdfefc4c0a51e37f6aba0cd114f520868eb7e Mon Sep 17 00:00:00 2001 From: IAmTomahawkx Date: Mon, 4 Mar 2024 15:37:19 -0800 Subject: [PATCH] potential fix for bug with headers not getting set after token updates --- twitchio/http.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/twitchio/http.py b/twitchio/http.py index 2276af5c..fe90ca21 100644 --- a/twitchio/http.py +++ b/twitchio/http.py @@ -121,22 +121,8 @@ async def request(self, route: Route, *, paginate=True, limit=100, full_body=Fal raise errors.NoClientID("A Client ID is required to use the Twitch API") headers = route.headers or {} - if force_app_token and "Authorization" not in headers: - if not self.client_secret: - raise errors.NoToken( - "An app access token is required for this route, please provide a client id and client secret" - ) - if self.app_token is None: - await self._generate_login() - headers["Authorization"] = f"Bearer {self.app_token}" - elif not self.token and not self.client_secret and "Authorization" not in headers: - raise errors.NoToken( - "Authorization is required to use the Twitch API. Pass token and/or client_secret to the Client constructor" - ) - if "Authorization" not in headers: - if not self.token: - await self._generate_login() - headers["Authorization"] = f"Bearer {self.token}" + await self._apply_auth(headers, force_app_token, False) + headers["Client-ID"] = self.client_id if not self.session: @@ -165,7 +151,7 @@ def get_limit(): q = [("after", cursor), *q] q = [("first", get_limit()), *q] path = path.with_query(q) - body, is_text = await self._request(route, path, headers) + body, is_text = await self._request(route, path, headers, force_app_token=force_app_token) if is_text: return body if full_body: @@ -182,7 +168,26 @@ def get_limit(): is_finished = reached_limit() if limit is not None else True if paginate else True return data - async def _request(self, route, path, headers, utilize_bucket=True): + async def _apply_auth(self, headers: dict, force_app_token: bool, force_apply: bool) -> None: + if force_app_token and "Authorization" not in headers: + if not self.client_secret: + raise errors.NoToken( + "An app access token is required for this route, please provide a client id and client secret" + ) + if self.app_token is None: + await self._generate_login() + headers["Authorization"] = f"Bearer {self.app_token}" + elif not self.token and not self.client_secret and "Authorization" not in headers: + raise errors.NoToken( + "Authorization is required to use the Twitch API. Pass token and/or client_secret to the Client constructor" + ) + if "Authorization" not in headers or force_apply: + if not self.token: + await self._generate_login() + + headers["Authorization"] = f"Bearer {self.token}" + + async def _request(self, route, path, headers, utilize_bucket=True, force_app_token: bool = False): reason = None for attempt in range(5): @@ -224,6 +229,7 @@ async def _request(self, route, path, headers, utilize_bucket=True): if "Invalid OAuth token" in message_json.get("message", ""): try: await self._generate_login() + await self._apply_auth(headers, force_app_token, True) continue except: raise errors.Unauthorized(