Mr-Help commited on
Commit
e4eb62c
·
verified ·
1 Parent(s): fca2d0b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +34 -6
main.py CHANGED
@@ -1,8 +1,14 @@
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import JSONResponse
 
 
3
 
4
  app = FastAPI()
5
 
 
 
 
 
6
  @app.get("/auth/callback")
7
  async def tiktok_auth_callback(request: Request):
8
  code = request.query_params.get("code")
@@ -14,13 +20,35 @@ async def tiktok_auth_callback(request: Request):
14
  content={"message": "Missing 'code' parameter from TikTok OAuth."}
15
  )
16
 
17
- print("✅ Received TikTok OAuth code:", code)
18
- print("📦 State parameter (if any):", state)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  return JSONResponse(
21
  content={
22
- "message": "OAuth code received successfully.",
23
- "code_received": code,
24
- "note": "Token exchange will be implemented after app approval."
25
  }
26
- )
 
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")
 
20
  content={"message": "Missing 'code' parameter from TikTok OAuth."}
21
  )
22
 
23
+ # Exchange code for tokens
24
+ token_url = "https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/"
25
+ payload = {
26
+ "app_id": CLIENT_KEY,
27
+ "secret": CLIENT_SECRET,
28
+ "auth_code": code
29
+ }
30
+
31
+ response = requests.post(token_url, json=payload)
32
+ data = response.json()
33
+
34
+ if data.get("code") != 0:
35
+ return JSONResponse(
36
+ status_code=400,
37
+ content={"message": "Token exchange failed", "details": data}
38
+ )
39
+
40
+ access_token = data["data"]["access_token"]
41
+ refresh_token = data["data"]["refresh_token"]
42
+ advertiser_ids = data["data"]["advertiser_ids"]
43
+
44
+ print("✅ Access Token:", access_token)
45
+ print("🔁 Refresh Token:", refresh_token)
46
+ print("📢 Advertiser IDs:", advertiser_ids)
47
 
48
  return JSONResponse(
49
  content={
50
+ "message": "Token exchange successful",
51
+ "access_token": access_token,
52
+ "advertiser_ids": advertiser_ids
53
  }
54
+ )