본문 바로가기

Python

TISTORY Python 자동으로 Access Token 받아오기.

  •  TISTORY 오픈 API 앱 등록.

TISTORY 오픈 API 설정

- 앱 등록

  서비스 명 입력

  서비스 URL 및 CallBack에 TISTORY 주소 입력한다.

  등록

TISTORY OPEN API APP등록

 

등록 완료 후 App ID 및 Secret Key 사용 가능.

TISTORY APPID 및 Secret Key 확인 가능.

위의 정보들이 Python에서 필요하다.

앱 등록 시 입력한 정보로 아래 값 설정.

g_client_id = 
g_redirect_uri = 

g_user_id = 
g_password =

 

Python 소스

import requests
import json
import re

#Open API 앱 등록후 App 아이디 설정
g_client_id = {App ID}
#Open API 앱 등록후 입력한 CallBack 설정
g_redirect_uri = {CallBack}
g_oauth_url = "https://www.tistory.com/oauth/authorize?client_id="+g_client_id+"&redirect_uri="+g_redirect_uri+"&response_type=token"
#TISTORY ID
g_user_id = {TISTORY ID}
#TISTORY PASSWORD
g_password = {TISTORY PASSWORD}

print(g_oauth_url)
res = requests.get(g_oauth_url)
print(res.status_code)
print(res.headers['Set-Cookie'])
kakao_cookie = res.headers['Set-Cookie'].replace("; path=/",'')
print(kakao_cookie)
print(res.url)
headers = {
    'Accept' : 'text/html, application/xhtml+xml, image/jxr, */*',
    'Accept-Encoding' : 'gzip, deflate',
    'Accept-Language' : 'ko, en-US; q=0.8, en; q=0.6, zh-Hans-CN; q=0.4, zh-Hans; q=0.2',
    'Cache-Control' :  'no-cache',
    'Connection' : 'Keep-Alive',
    'Content-Type' : 'application/x-www-form-urlencoded',
    'Cookie' : kakao_cookie,
    'Host' : 'www.tistory.com',
    'Referer' : res.url,
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
login_data = {
    'fp' : 'a668ac102f81b86f521c07dfb6dc992c',
    'keepLogin' : 'on',
    'loginId' : g_user_id,
    'password' : g_password,
    'redirectUrl' : res.url
    }
res = requests.post('https://www.tistory.com/auth/login', headers=headers, data=login_data)
print(res.url)
match = re.match('(.*?)access_token=(?P<access_token>.*?)&state=', res.url)
gd = match.groupdict()
access_token = gd['access_token']
print(access_token)

 

python 실행 후 access token 확인

 

TISTORY Access Token 확인.

 

관련 소스

https://github.com/leechul/py_tistory

 

leechul/py_tistory

Tistory OpenAPI Python automation. Contribute to leechul/py_tistory development by creating an account on GitHub.

github.com

처음 사용시 이메일에서 로그인 확인하여주어야 한다.