Lakpriya Seneviratna commited on
Commit
58f3513
·
1 Parent(s): 1dc6da8

chore: Update TikTok callback route to use new API endpoint

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -603,7 +603,10 @@ async def tiktok_callback(request: Request):
603
 
604
  # Exchange code for access token
605
  response = requests.post(
606
- 'https://www.tiktok.com/v2/auth/token/',
 
 
 
607
  data={
608
  'client_key': CLIENT_KEY,
609
  'client_secret': CLIENT_SECRET,
@@ -613,25 +616,31 @@ async def tiktok_callback(request: Request):
613
  }
614
  )
615
 
616
- # Print the raw response for debugging
617
- print(f"Raw response text: {response.text}")
 
618
 
619
  # Try parsing the JSON response
620
  token_response = response.json()
621
 
 
622
  if "error" in token_response:
623
- raise HTTPException(status_code=400, detail=token_response["error_description"])
 
 
 
624
 
625
- access_token = token_response.get('data').get('access_token')
626
- open_id = token_response.get('data').get('open_id')
 
627
 
 
628
  return {"message": "Authorization successful", "access_token": access_token, "open_id": open_id}
629
-
630
  except Exception as e:
631
- # Log the exception
632
  print(f"Exception occurred: {str(e)}")
633
  raise HTTPException(status_code=500, detail="Internal Server Error")
634
-
635
  @app.get("/generate_video")
636
  def generate_video():
637
  try:
 
603
 
604
  # Exchange code for access token
605
  response = requests.post(
606
+ 'https://open.tiktokapis.com/v2/oauth/token/',
607
+ headers={
608
+ 'Content-Type': 'application/x-www-form-urlencoded'
609
+ },
610
  data={
611
  'client_key': CLIENT_KEY,
612
  'client_secret': CLIENT_SECRET,
 
616
  }
617
  )
618
 
619
+ # Check for successful response
620
+ if response.status_code != 200:
621
+ raise HTTPException(status_code=response.status_code, detail="Failed to exchange code for token")
622
 
623
  # Try parsing the JSON response
624
  token_response = response.json()
625
 
626
+ # Check if there is an error in the response
627
  if "error" in token_response:
628
+ raise HTTPException(status_code=400, detail=token_response.get("error_description", "Unknown error"))
629
+
630
+ access_token = token_response.get('access_token')
631
+ open_id = token_response.get('open_id')
632
 
633
+ # Log the access token and open_id for debugging purposes
634
+ print(f"Access token: {access_token}")
635
+ print(f"Open ID: {open_id}")
636
 
637
+ # Return a success message with the token details
638
  return {"message": "Authorization successful", "access_token": access_token, "open_id": open_id}
639
+
640
  except Exception as e:
641
+ # Log the exception for debugging purposes
642
  print(f"Exception occurred: {str(e)}")
643
  raise HTTPException(status_code=500, detail="Internal Server Error")
 
644
  @app.get("/generate_video")
645
  def generate_video():
646
  try: