Mr-Help commited on
Commit
3359437
·
verified ·
1 Parent(s): 4af89d8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +40 -21
main.py CHANGED
@@ -1,35 +1,54 @@
1
  from fastapi import FastAPI, Request
 
2
  import requests
 
3
 
4
  app = FastAPI()
5
 
6
- CLIENT_KEY = "7502238777792790545"
7
- CLIENT_SECRET = "4db49a3c142fd84f41c099e9cdbb0949de00cff0"
8
- REDIRECT_URI = "https://mr-help-binrushd-tiktok-authenticator.hf.space/auth/callback"
9
 
10
  @app.get("/auth/callback")
11
- async def auth_callback(request: Request):
12
  code = request.query_params.get("code")
 
13
 
14
- token_url = "https://open.tiktokapis.com/v2/oauth/token"
 
 
 
 
 
 
15
  payload = {
16
- "client_key": "7502238777792790545",
17
- "client_secret": "YOUR_CLIENT_SECRET",
18
- "code": code,
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, data=payload, headers=headers)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- try:
29
- return response.json()
30
- except Exception:
31
- return {
32
- "error": "Failed to parse TikTok token response",
33
- "status_code": response.status_code,
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
+ )