Skip to content

Commit 99a6c4b

Browse files
committed
fix: update token retrieval process to improve reliability and error handling (resolves #100, resolves #103)
1 parent 3867014 commit 99a6c4b

1 file changed

Lines changed: 39 additions & 43 deletions

File tree

TokenRetriever.py

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import re
12
import time
23
from seleniumbase import SB
3-
from selenium.webdriver.support.ui import WebDriverWait
44
import json
55
import requests
66
import hashlib
@@ -16,9 +16,9 @@ def __init__(self, cookies_file='cookies.json'):
1616
self.code_verifier = self.generate_code_verifier()
1717
self.code_challenge = self.generate_code_challenge(self.code_verifier)
1818
self.streamlabs_auth_url = (
19-
f"https://streamlabs.com/m/login?"
20-
f"force_verify=1&external=mobile&skip_splash=1&tiktok"
21-
f"&code_challenge={self.code_challenge}"
19+
f"https://streamlabs.com/slobs/login?"
20+
f"skip_splash=true&external=electron&tiktok=&force_verify=&origin=slobs"
21+
f"&code_challenge={self.code_challenge}&code_flow=true"
2222
)
2323
self.cookies_file = cookies_file
2424
self.auth_code = None
@@ -40,51 +40,47 @@ def load_cookies(self, driver):
4040
driver.add_cookie(cookie)
4141

4242
def retrieve_token(self, binary_location=None):
43-
with SB(uc=True, headless=False, binary_location=binary_location) as sb:
43+
code = ""
44+
45+
with SB(wire=True, headless=False, binary_location=binary_location) as sb:
4446
sb.open("https://www.tiktok.com/transparency")
4547
self.load_cookies(sb)
4648

4749
sb.open(self.streamlabs_auth_url)
48-
50+
4951
try:
50-
wait = WebDriverWait(sb, 600)
51-
wait.until(lambda sb: "success=true" in sb.get_current_url())
52+
pattern = re.compile(r"^https://streamlabs\.com/tiktok/auth")
53+
request = sb.driver.wait_for_request(pattern, timeout=600)
54+
if request:
55+
code = request.response.headers.get("Location", "").split("code=")[-1]
5256
except:
53-
print("Failed to authorize TikTok.")
54-
return None
55-
56-
params = {
57-
'client_key': self.CLIENT_KEY,
58-
'scope': 'user.info.basic,live.room.tag,live.room.info,live.room.manage,user.info.profile,user.info.stats',
59-
'aid': '1459',
60-
'redirect_uri': self.REDIRECT_URI,
61-
'source': 'web',
62-
'response_type': 'code'
63-
}
64-
with requests.Session() as s:
65-
try:
66-
time.sleep(5)
67-
params= {
68-
"code_verifier": self.code_verifier
69-
}
70-
response = s.get(self.STREAMLABS_API_URL, params=params)
71-
if response.status_code != 200:
72-
print(f"Bad response: {response.status_code} - {response.text}")
73-
return None
74-
57+
print("Timed out waiting for Streamlabs redirect.")
58+
if code:
59+
with requests.Session() as s:
7560
try:
76-
resp_json = response.json()
77-
except json.JSONDecodeError:
78-
print("Invalid JSON response. Status code:", response.status_code)
79-
return None
80-
if resp_json.get("success"):
81-
token = resp_json["data"].get("oauth_token")
82-
print(f"Got Streamlabs OAuth token: {token}")
83-
return token
84-
else:
85-
print("Streamlabs token request failed:", resp_json)
61+
time.sleep(5)
62+
params= {
63+
"code_verifier": self.code_verifier,
64+
"code": code
65+
}
66+
response = s.get(self.STREAMLABS_API_URL, params=params)
67+
if response.status_code != 200:
68+
print(f"Bad response: {response.status_code} - {response.text}")
69+
return None
70+
71+
try:
72+
resp_json = response.json()
73+
except json.JSONDecodeError:
74+
print("Invalid JSON response. Status code:", response.status_code)
75+
return None
76+
if resp_json.get("success"):
77+
token = resp_json["data"].get("oauth_token")
78+
print(f"Got Streamlabs OAuth token: {token}")
79+
return token
80+
else:
81+
print("Streamlabs token request failed:", resp_json)
82+
return None
83+
except Exception as e:
84+
print("Error requesting token from Streamlabs:", e)
8685
return None
87-
except Exception as e:
88-
print("Error requesting token from Streamlabs:", e)
89-
return None
9086
return None

0 commit comments

Comments
 (0)