Update main.py
Browse files
main.py
CHANGED
|
@@ -1,35 +1,54 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
@app.get("/auth/callback")
|
| 11 |
-
async def
|
| 12 |
code = request.query_params.get("code")
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
payload = {
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"grant_type": "authorization_code",
|
| 20 |
-
"redirect_uri": "https://mr-help-binrushd-tiktok-authenticator.hf.space/auth/callback"
|
| 21 |
-
}
|
| 22 |
-
headers = {
|
| 23 |
-
"Content-Type": "application/x-www-form-urlencoded"
|
| 24 |
}
|
| 25 |
|
| 26 |
-
response = requests.post(token_url,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
"text": response.text
|
| 35 |
}
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
import requests
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Replace with your real TikTok credentials
|
| 9 |
+
CLIENT_KEY = os.getenv("TIKTOK_CLIENT_KEY", "YOUR_APP_ID")
|
| 10 |
+
CLIENT_SECRET = os.getenv("TIKTOK_CLIENT_SECRET", "YOUR_APP_SECRET")
|
| 11 |
|
| 12 |
@app.get("/auth/callback")
|
| 13 |
+
async def tiktok_auth_callback(request: Request):
|
| 14 |
code = request.query_params.get("code")
|
| 15 |
+
state = request.query_params.get("state")
|
| 16 |
|
| 17 |
+
if not code:
|
| 18 |
+
return JSONResponse(
|
| 19 |
+
status_code=400,
|
| 20 |
+
content={"message": "Missing 'code' parameter from TikTok OAuth."}
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
token_url = "https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/"
|
| 24 |
payload = {
|
| 25 |
+
"app_id": CLIENT_KEY,
|
| 26 |
+
"secret": CLIENT_SECRET,
|
| 27 |
+
"auth_code": code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
+
response = requests.post(token_url, json=payload)
|
| 31 |
+
data = response.json()
|
| 32 |
+
|
| 33 |
+
if data.get("code") != 0:
|
| 34 |
+
return JSONResponse(
|
| 35 |
+
status_code=400,
|
| 36 |
+
content={"message": "Token exchange failed", "details": data}
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
access_token = data["data"].get("access_token")
|
| 40 |
+
refresh_token = data["data"].get("refresh_token", None) # ✅ safe fallback
|
| 41 |
+
advertiser_ids = data["data"].get("advertiser_ids", [])
|
| 42 |
+
|
| 43 |
+
print("✅ Access Token:", access_token)
|
| 44 |
+
print("🔁 Refresh Token:", refresh_token)
|
| 45 |
+
print("📢 Advertiser IDs:", advertiser_ids)
|
| 46 |
|
| 47 |
+
return JSONResponse(
|
| 48 |
+
content={
|
| 49 |
+
"message": "Token exchange successful",
|
| 50 |
+
"access_token": access_token,
|
| 51 |
+
"refresh_token": refresh_token,
|
| 52 |
+
"advertiser_ids": advertiser_ids
|
|
|
|
| 53 |
}
|
| 54 |
+
)
|