kv-tiktok/backend/debug_api.py

44 lines
1.6 KiB
Python

import asyncio
import httpx
import sys
from core.playwright_manager import PlaywrightManager
async def test_api():
print("Loading credentials...")
cookies, user_agent = PlaywrightManager.load_stored_credentials()
headers = {
"User-Agent": user_agent or "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Referer": "https://www.tiktok.com/",
"Cookie": "; ".join([f"{c['name']}={c['value']}" for c in cookies]),
}
username = "x.ka.baongoc"
url = f"https://www.tiktok.com/api/user/detail/?uniqueId={username}"
print(f"Fetching {url}...")
async with httpx.AsyncClient(timeout=10.0, follow_redirects=True) as client:
res = await client.get(url, headers=headers)
print(f"Status: {res.status_code}")
if res.status_code == 200:
try:
data = res.json()
user = data.get("userInfo", {}).get("user", {})
sec_uid = user.get("secUid")
print(f"SecUid: {sec_uid}")
if not sec_uid:
print("Response body preview:", str(data)[:500])
except:
print("JSON Decode Failed. Content preview:")
print(res.text[:500])
else:
print("Response:", res.text[:500])
if __name__ == "__main__":
try:
if sys.platform == "win32":
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
loop.run_until_complete(test_api())
except Exception as e:
print(e)