Skip to content

Commit fc7cdfe

Browse files
committed
potential fix for bug with headers not getting set after token updates
1 parent 815f9b5 commit fc7cdfe

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

twitchio/http.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,8 @@ async def request(self, route: Route, *, paginate=True, limit=100, full_body=Fal
121121
raise errors.NoClientID("A Client ID is required to use the Twitch API")
122122
headers = route.headers or {}
123123

124-
if force_app_token and "Authorization" not in headers:
125-
if not self.client_secret:
126-
raise errors.NoToken(
127-
"An app access token is required for this route, please provide a client id and client secret"
128-
)
129-
if self.app_token is None:
130-
await self._generate_login()
131-
headers["Authorization"] = f"Bearer {self.app_token}"
132-
elif not self.token and not self.client_secret and "Authorization" not in headers:
133-
raise errors.NoToken(
134-
"Authorization is required to use the Twitch API. Pass token and/or client_secret to the Client constructor"
135-
)
136-
if "Authorization" not in headers:
137-
if not self.token:
138-
await self._generate_login()
139-
headers["Authorization"] = f"Bearer {self.token}"
124+
await self._apply_auth(headers, force_app_token, False)
125+
140126
headers["Client-ID"] = self.client_id
141127

142128
if not self.session:
@@ -165,7 +151,7 @@ def get_limit():
165151
q = [("after", cursor), *q]
166152
q = [("first", get_limit()), *q]
167153
path = path.with_query(q)
168-
body, is_text = await self._request(route, path, headers)
154+
body, is_text = await self._request(route, path, headers, force_app_token=force_app_token)
169155
if is_text:
170156
return body
171157
if full_body:
@@ -182,7 +168,26 @@ def get_limit():
182168
is_finished = reached_limit() if limit is not None else True if paginate else True
183169
return data
184170

185-
async def _request(self, route, path, headers, utilize_bucket=True):
171+
async def _apply_auth(self, headers: dict, force_app_token: bool, force_apply: bool) -> None:
172+
if force_app_token and "Authorization" not in headers:
173+
if not self.client_secret:
174+
raise errors.NoToken(
175+
"An app access token is required for this route, please provide a client id and client secret"
176+
)
177+
if self.app_token is None:
178+
await self._generate_login()
179+
headers["Authorization"] = f"Bearer {self.app_token}"
180+
elif not self.token and not self.client_secret and "Authorization" not in headers:
181+
raise errors.NoToken(
182+
"Authorization is required to use the Twitch API. Pass token and/or client_secret to the Client constructor"
183+
)
184+
if "Authorization" not in headers or force_apply:
185+
if not self.token:
186+
await self._generate_login()
187+
188+
headers["Authorization"] = f"Bearer {self.token}"
189+
190+
async def _request(self, route, path, headers, utilize_bucket=True, force_app_token: bool = False):
186191
reason = None
187192

188193
for attempt in range(5):
@@ -224,6 +229,7 @@ async def _request(self, route, path, headers, utilize_bucket=True):
224229
if "Invalid OAuth token" in message_json.get("message", ""):
225230
try:
226231
await self._generate_login()
232+
await self._apply_auth(headers, force_app_token, True)
227233
continue
228234
except:
229235
raise errors.Unauthorized(

0 commit comments

Comments
 (0)