File size: 1,661 Bytes
96e900a 3359437 e4eb62c 3359437 96e900a 3359437 e4eb62c 96e900a 3359437 96e900a 3359437 96e900a 3359437 e4eb62c 3359437 dd5e6bc 1ff4af4 3359437 96e900a 3359437 fca2d0b 3359437 | 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 46 47 48 49 50 51 52 53 54 | from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import requests
import os
app = FastAPI()
# Replace with your real TikTok credentials
CLIENT_KEY = os.getenv("TIKTOK_CLIENT_KEY", "YOUR_APP_ID")
CLIENT_SECRET = os.getenv("TIKTOK_CLIENT_SECRET", "YOUR_APP_SECRET")
@app.get("/auth/callback")
async def tiktok_auth_callback(request: Request):
code = request.query_params.get("code")
state = request.query_params.get("state")
if not code:
return JSONResponse(
status_code=400,
content={"message": "Missing 'code' parameter from TikTok OAuth."}
)
token_url = "https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/"
payload = {
"app_id": CLIENT_KEY,
"secret": CLIENT_SECRET,
"auth_code": code
}
response = requests.post(token_url, json=payload)
data = response.json()
if data.get("code") != 0:
return JSONResponse(
status_code=400,
content={"message": "Token exchange failed", "details": data}
)
access_token = data["data"].get("access_token")
refresh_token = data["data"].get("refresh_token", None) # ✅ safe fallback
advertiser_ids = data["data"].get("advertiser_ids", [])
print("✅ Access Token:", access_token)
print("🔁 Refresh Token:", refresh_token)
print("📢 Advertiser IDs:", advertiser_ids)
return JSONResponse(
content={
"message": "Token exchange successful",
"access_token": access_token,
"refresh_token": refresh_token,
"advertiser_ids": advertiser_ids
}
) |