@@ -93,6 +93,7 @@ def __init__(
9393 self .client = client
9494 self .session = None
9595 self .token = api_token
96+ self .app_token = None
9697 self ._refresh_token = None
9798 self .client_secret = client_secret
9899 self .client_id = client_id
@@ -101,7 +102,7 @@ def __init__(
101102 self .bucket = RateBucket (method = "http" )
102103 self .scopes = kwargs .get ("scopes" , [])
103104
104- async def request (self , route : Route , * , paginate = True , limit = 100 , full_body = False ):
105+ async def request (self , route : Route , * , paginate = True , limit = 100 , full_body = False , force_app_token = False ):
105106 """
106107 Fulfills an API request
107108
@@ -115,6 +116,8 @@ async def request(self, route: Route, *, paginate=True, limit=100, full_body=Fal
115116 The data limit per request when paginating. Defaults to 100
116117 full_body : class:`bool`
117118 Whether to return the full response body or to accumulate the `data` key. Defaults to False. `paginate` must be False if this is True.
119+ force_app_token : :class:`bool`
120+ Forcibly use the client_id and client_secret generated token, if available. Otherwise fail the request immediately
118121 """
119122 if full_body :
120123 assert not paginate
@@ -127,9 +130,19 @@ async def request(self, route: Route, *, paginate=True, limit=100, full_body=Fal
127130
128131 headers = route .headers or {}
129132
130- if not self .token and not self .client_secret and "Authorization" not in headers :
133+ if force_app_token and "Authorization" not in headers :
134+ if not self .client_secret :
135+ raise errors .NoToken (
136+ "An app access token is required for this route, please provide a client id and client secret"
137+ )
138+
139+ if self .app_token is None :
140+ await self ._generate_login ()
141+ headers ["Authorization" ] = f"Bearer { self .app_token } "
142+
143+ elif not self .token and not self .client_secret and "Authorization" not in headers :
131144 raise errors .NoToken (
132- "Authorization is required to use the Twitch API. Pass api_token and/or client_secret to the Client constructor"
145+ "Authorization is required to use the Twitch API. Pass token and/or client_secret to the Client constructor"
133146 )
134147
135148 if "Authorization" not in headers :
@@ -257,7 +270,7 @@ async def _generate_login(self):
257270 token = await self .client .event_token_expired ()
258271 if token is not None :
259272 assert isinstance (token , str ), TypeError (f"Expected a string, got { type (token )} " )
260- self .token = token
273+ self .token = self . app_token = token
261274 return
262275 except Exception as e :
263276 self .client .run_event ("error" , e )
@@ -288,7 +301,7 @@ async def _generate_login(self):
288301 raise errors .HTTPException ("Unable to generate a token: " + await resp .text ())
289302
290303 data = await resp .json ()
291- self .token = data ["access_token" ]
304+ self .token = self . app_token = data ["access_token" ]
292305 self ._refresh_token = data .get ("refresh_token" , None )
293306 logger .info ("Invalid or no token found, generated new token: %s" , self .token )
294307
0 commit comments