Spaces:
Sleeping
Sleeping
Lakpriya Seneviratna
commited on
Commit
·
1dc6da8
1
Parent(s):
816ed38
chore: Update TikTok callback route in FastAPI
Browse files
app.py
CHANGED
@@ -598,16 +598,11 @@ async def tiktok_callback(request: Request):
|
|
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 |
-
|
611 |
'https://www.tiktok.com/v2/auth/token/',
|
612 |
data={
|
613 |
'client_key': CLIENT_KEY,
|
@@ -616,10 +611,13 @@ async def tiktok_callback(request: Request):
|
|
616 |
'grant_type': 'authorization_code',
|
617 |
'redirect_uri': REDIRECT_URI
|
618 |
}
|
619 |
-
)
|
620 |
|
621 |
-
#
|
622 |
-
print(f"
|
|
|
|
|
|
|
623 |
|
624 |
if "error" in token_response:
|
625 |
raise HTTPException(status_code=400, detail=token_response["error_description"])
|
@@ -627,10 +625,6 @@ async def tiktok_callback(request: Request):
|
|
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:
|
|
|
598 |
state = request.query_params.get('state')
|
599 |
csrf_state = request.cookies.get('csrf_state')
|
600 |
|
|
|
|
|
|
|
|
|
|
|
601 |
if state != csrf_state:
|
602 |
raise HTTPException(status_code=400, detail="Invalid state parameter")
|
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,
|
|
|
611 |
'grant_type': 'authorization_code',
|
612 |
'redirect_uri': REDIRECT_URI
|
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"])
|
|
|
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:
|