728x90
반응형
틱톡api 를 사용하기 위해서는 토큰이 필요한데 이 토큰을 발급받는 함수이다.
client_key, client_secret, redirect_uri, code 가 필요하며
발급된 토큰의 유효기간은 1일이다.
def getToken(client_key, client_secret, redirect_uri, code):
url = "https://open.tiktokapis.com/v2/oauth/token/"
# 요청에 보낼 데이터 (PHP의 http_build_query와 동일하게 처리됨)
payload = {
'client_key': client_key,
'client_secret': client_secret,
'code': code,
'grant_type': 'authorization_code',
'redirect_uri': redirect_uri
}
# 헤더 설정
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
}
try:
# POST 요청 실행
# data 파라미터에 딕셔너리를 넣으면 자동으로 application/x-www-form-urlencoded로 인코딩됩니다.
response = requests.post(url, data=payload, headers=headers)
# 응답 상태 코드 확인
http_code = response.status_code
result = response.json() # JSON 응답 파싱
if http_code == 200 and 'error' not in result:
print(f"토큰 발급 성공 : {result}")
return result
else:
print(f"토큰 발급 실패: {result}")
return "0"
except requests.exceptions.RequestException as e:
# PHP의 curl_errno와 유사한 예외 처리
print(f"Request error: {e}")
return "0"

728x90
반응형
'파이썬' 카테고리의 다른 글
| 날짜처리하는 방법 리스트 (0) | 2026.01.15 |
|---|---|
| 틱톡 토큰 갱신하기 (0) | 2026.01.14 |
| json 파일 생성, 읽기, 수정 (0) | 2026.01.14 |
| 조건문에서 널값 체크하기 (0) | 2025.02.07 |
| 2차원 배열 모두 출력하기 (0) | 2025.02.05 |