Spaces:
Sleeping
Sleeping
from flask import Flask, request, redirect | |
import requests | |
import random | |
app = Flask(__name__) | |
CLIENT_ID = 'awfh19twbfyt0de0' | |
CLIENT_SECRET = 'awfh19twbfyt0de0' | |
REDIRECT_URI = 'https://127.0.0.1:5000/callback/' # This must match the redirect URI registered in TikTok Developer Portal | |
def home(): | |
csrfState = format(random.randint(0, 36**16), 'x') | |
# Redirect the user to TikTok's authorization page | |
tiktok_auth_url = f"https://open-api.tiktok.com/platform/oauth/connect/?client_key={CLIENT_ID}&scope=user.info.basic,video.list&response_type=code&redirect_uri={REDIRECT_URI}&state={csrfState}" | |
return redirect(tiktok_auth_url) | |
def callback(): | |
# Get the code from the callback URL | |
code = request.args.get('code') | |
# Exchange the code for an access token | |
token_url = "https://open-api.tiktok.com/oauth/access_token/" | |
payload = { | |
'client_key': CLIENT_ID, | |
'client_secret': CLIENT_SECRET, | |
'code': code, | |
'grant_type': 'authorization_code', | |
} | |
response = requests.post(token_url, data=payload) | |
access_token = response.json().get('data', {}).get('access_token') | |
return f"Access Token: {access_token}" | |
if __name__ == '__main__': | |
app.run(debug=True) | |