Skip to content

Commit

Permalink
potential fix for bug with headers not getting set after token updates
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmTomahawkx committed Mar 4, 2024
1 parent 815f9b5 commit fc7cdfe
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions twitchio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit fc7cdfe

Please sign in to comment.