728x90
반응형
발급받은 토큰은 하루만 사용할 수 있는데
기간을 연장하려면 refresh_token, client_key, client_secret 를 사용해서 갱신해야한다
아래는 함수이다.
def reNewToken(refresh_token,client_key, client_secret):
url = "https://open.tiktokapis.com/v2/oauth/token/"
payload = {
'client_key': client_key,
'client_secret': client_secret,
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}
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
반응형
'파이썬' 카테고리의 다른 글
| 파이썬 mysql 처리 클래스 (0) | 2026.01.19 |
|---|---|
| 날짜처리하는 방법 리스트 (0) | 2026.01.15 |
| 틱톡 토큰 발급받기 (0) | 2026.01.14 |
| json 파일 생성, 읽기, 수정 (0) | 2026.01.14 |
| 조건문에서 널값 체크하기 (0) | 2025.02.07 |