Судя по тому, что я получил с их веб-сайта, сначала вам нужно отправить пользователя на определенную ссылку с вашим client_key. и redirect_uri, то пользователю необходимо войти в систему.
После того, как он войдет в систему, в URL-адресе возврата будет код, который вы можете использовать для получения access_token пользователя. Проблема в том, что я всегда получал ошибку:
{'error': 'invalid_grant', 'error_description': 'Срок действия кода авторизации истек.', 'log_id ': 'XXXXXXXX'
Вот ссылка, которая объясняет все, что я сделал:
- Чтобы разрешить пользователю подключиться и получить код: https://developers.tiktok.com/doc/login ... navigation
- Чтобы получить доступ пользователя токен: https://developers.tiktok.com/doc/oauth ... anagement/
Вот код Python, который я сделал, чтобы проверить это:
Код: Выделить всё
import requests
import urllib
import secrets
import time
# Constants
CLIENT_KEY = "xxxxx"
CLIENT_SECRET = "yyyyy"
REDIRECT_URI = "https://oxyfoo.com/pierre/tiktok-automation/"
AUTHORIZE_URL = 'https://www.tiktok.com/v2/auth/authorize/'
TOKEN_URL = 'https://open.tiktokapis.com/v2/oauth/token/'
# Function to generate authorization URL
def generate_auth_url():
csrf_state = secrets.token_hex(16)
params = {
'client_key': CLIENT_KEY,
'scope': 'user.info.basic',
'response_type': 'code',
'redirect_uri': REDIRECT_URI,
'state': csrf_state,
}
url = AUTHORIZE_URL + '?' + urllib.parse.urlencode(params)
return url, csrf_state
# Function to get the access token
def get_access_token(authorization_code):
data = {
'client_key': CLIENT_KEY,
'client_secret': CLIENT_SECRET,
'code': authorization_code,
'grant_type': 'authorization_code',
'redirect_uri': REDIRECT_URI
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.post(TOKEN_URL, headers=headers, data=urllib.parse.urlencode(data))
if response.status_code == 200:
return response.json()
else:
print(f"Error: Received status code {response.status_code}")
print(f"Response content: {response.content.decode()}")
return None
# Testing
def manual_test():
# Generate authorization URL
print("Generating authorization URL...")
url, state = generate_auth_url()
print("URL:", url) # I go there and log in
print("State:", state)
# Prompt user for the redirect URL
input_ = input("Paste the URL you were redirected to: ") # I put the url of the page that start with my redirect_uri with ?code, scopes, etc here
# Extract authorization code from redirect URL
code = input_.split("code=")[1].split("&")[0]
print("Code:", code)
# Fetch access token without delay
print("Fetching access token...")
token_info = get_access_token(code)
print(token_info) # Here, I always have the error invalid grant authorization code is expired.
if __name__ == "__main__":
manual_test()
Подробнее здесь: https://stackoverflow.com/questions/768 ... ys-expired