Lakpriya Seneviratna commited on
Commit
816ed38
·
1 Parent(s): d279001

chore: Refactor TikTok login and callback routes in FastAPI

Browse files
Files changed (1) hide show
  1. app.py +40 -24
app.py CHANGED
@@ -593,34 +593,50 @@ async def tiktok_login():
593
 
594
  @app.get("/tiktok_callback")
595
  async def tiktok_callback(request: Request):
596
- code = request.query_params.get('code')
597
- state = request.query_params.get('state')
598
- csrf_state = request.cookies.get('csrf_state')
599
-
600
- if state != csrf_state:
601
- raise HTTPException(status_code=400, detail="Invalid state parameter")
602
-
603
- # Exchange code for access token
604
- token_response = requests.post(
605
- 'https://www.tiktok.com/v2/auth/token/',
606
- data={
607
- 'client_key': CLIENT_KEY,
608
- 'client_secret': CLIENT_SECRET,
609
- 'code': code,
610
- 'grant_type': 'authorization_code',
611
- 'redirect_uri': REDIRECT_URI
612
- }
613
- ).json()
 
 
 
 
 
 
614
 
615
- if "error" in token_response:
616
- raise HTTPException(status_code=400, detail=token_response["error_description"])
617
 
618
- access_token = token_response.get('data').get('access_token')
619
- open_id = token_response.get('data').get('open_id')
620
 
621
- # Save access_token and open_id for future use (Consider storing securely in your database)
 
622
 
623
- return {"message": "Authorization successful", "access_token": access_token, "open_id": open_id}
 
 
 
 
 
 
 
 
 
624
 
625
  @app.get("/generate_video")
626
  def generate_video():
 
593
 
594
  @app.get("/tiktok_callback")
595
  async def tiktok_callback(request: Request):
596
+ try:
597
+ code = request.query_params.get('code')
598
+ state = request.query_params.get('state')
599
+ csrf_state = request.cookies.get('csrf_state')
600
+
601
+ # Debugging information
602
+ print(f"Received code: {code}")
603
+ print(f"Received state: {state}")
604
+ print(f"CSRF state from cookie: {csrf_state}")
605
+
606
+ if state != csrf_state:
607
+ raise HTTPException(status_code=400, detail="Invalid state parameter")
608
+
609
+ # Exchange code for access token
610
+ token_response = requests.post(
611
+ 'https://www.tiktok.com/v2/auth/token/',
612
+ data={
613
+ 'client_key': CLIENT_KEY,
614
+ 'client_secret': CLIENT_SECRET,
615
+ 'code': code,
616
+ 'grant_type': 'authorization_code',
617
+ 'redirect_uri': REDIRECT_URI
618
+ }
619
+ ).json()
620
 
621
+ # Log the token response for debugging
622
+ print(f"Token response: {token_response}")
623
 
624
+ if "error" in token_response:
625
+ raise HTTPException(status_code=400, detail=token_response["error_description"])
626
 
627
+ access_token = token_response.get('data').get('access_token')
628
+ open_id = token_response.get('data').get('open_id')
629
 
630
+ # Log access token and open_id
631
+ print(f"Access token: {access_token}")
632
+ print(f"Open ID: {open_id}")
633
+
634
+ return {"message": "Authorization successful", "access_token": access_token, "open_id": open_id}
635
+
636
+ except Exception as e:
637
+ # Log the exception
638
+ print(f"Exception occurred: {str(e)}")
639
+ raise HTTPException(status_code=500, detail="Internal Server Error")
640
 
641
  @app.get("/generate_video")
642
  def generate_video():