This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
45 lines (41 loc) · 1.4 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
from dotenv import load_dotenv
import requests
import logging
import os
logger = logging.getLogger(__name__)
load_dotenv()
class Auth:
def __init__(self):
self.token = None
self.get_token()
def get_token(self):
response = None
try:
response = requests.post(
"https://id.twitch.tv/oauth2/token",
json={
"client_id": os.getenv("CLIENT_ID"),
"client_secret": os.getenv("CLIENT_SECRET"),
"grant_type": "client_credentials",
},
headers={"content-type": "application/json"},
timeout=10,
)
response.raise_for_status()
except requests.ConnectionError as e:
logger.error(f"error while getting auth token {e}")
raise "token_error"
except requests.Timeout as e:
logger.error(f"timeout while getting auth token {e}")
raise "token_error"
except requests.HTTPError as e:
logger.error(f"auth request returned wrong data {e}")
raise "token_error"
finally:
if response:
tmp_json = response.json()
self.token = tmp_json["access_token"]
else:
logger.error("somehow the response object is None")
raise "token_error"