@@ -121,22 +121,8 @@ async def request(self, route: Route, *, paginate=True, limit=100, full_body=Fal
121
121
raise errors .NoClientID ("A Client ID is required to use the Twitch API" )
122
122
headers = route .headers or {}
123
123
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
+
140
126
headers ["Client-ID" ] = self .client_id
141
127
142
128
if not self .session :
@@ -165,7 +151,7 @@ def get_limit():
165
151
q = [("after" , cursor ), * q ]
166
152
q = [("first" , get_limit ()), * q ]
167
153
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 )
169
155
if is_text :
170
156
return body
171
157
if full_body :
@@ -182,7 +168,26 @@ def get_limit():
182
168
is_finished = reached_limit () if limit is not None else True if paginate else True
183
169
return data
184
170
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 ):
186
191
reason = None
187
192
188
193
for attempt in range (5 ):
@@ -224,6 +229,7 @@ async def _request(self, route, path, headers, utilize_bucket=True):
224
229
if "Invalid OAuth token" in message_json .get ("message" , "" ):
225
230
try :
226
231
await self ._generate_login ()
232
+ await self ._apply_auth (headers , force_app_token , True )
227
233
continue
228
234
except :
229
235
raise errors .Unauthorized (
0 commit comments