Skip to content

Commit 8dff0a7

Browse files
committed
alternate method to allowing app access token requests
1 parent 64e5402 commit 8dff0a7

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

twitchio/ext/eventsub/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class EventSubHTTP:
1515
def __init__(self, client: EventSubClient, token: Optional[str]):
1616
self._client = client
1717
self._http = client.client._http
18-
self._token = token or self._http.token
18+
self._token = token
1919

2020
async def create_subscription(self, event_type: Tuple[str, int, Type[EventData]], condition: Dict[str, str]):
2121
payload = {
@@ -25,7 +25,7 @@ async def create_subscription(self, event_type: Tuple[str, int, Type[EventData]]
2525
"transport": {"method": "webhook", "callback": self._client.route, "secret": self._client.secret},
2626
}
2727
route = Route("POST", "eventsub/subscriptions", body=payload, token=self._token)
28-
return await self._http.request(route, paginate=False)
28+
return await self._http.request(route, paginate=False, force_app_token=True)
2929

3030
async def delete_subscription(self, substription: Union[str, Subscription]):
3131
if isinstance(substription, models.Subscription):

twitchio/http.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)