Spaces:
Sleeping
Sleeping
Lakpriya Seneviratna
commited on
Commit
·
816ed38
1
Parent(s):
d279001
chore: Refactor TikTok login and callback routes in FastAPI
Browse files
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 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
'
|
612 |
-
|
613 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
614 |
|
615 |
-
|
616 |
-
|
617 |
|
618 |
-
|
619 |
-
|
620 |
|
621 |
-
|
|
|
622 |
|
623 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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():
|