TISTORY OAuth 인증이란?
TISTORY는 Open API를 활용한 개발 도중 부분적으로 필요했던 "인증"의 불편함을 해소하고자 OAuth 2.0 프로토콜을 지원합니다.
1. Server-side flow (JSP, PHP) - Authorization code 방식
JSP/Servlet, PHP등과 같은 Server-side 프로그래밍으로 인증을 구현할 경우 사용하기 적합한 인증 방식입니다.
01. 인증에 필요한 티스토리의 인증 요청용 URL 2가지
- 인증요청 URL : https://www.tistory.com/oauth/authorize
- access_token 발급 요청 URL : https://www.tistory.com/oauth/access_token
02. 인증 요청 단계 (client -> Tistory)
티스토리에게 최초로 클라이언트(=컨슈머)가 인증을 요청합니다.
- URL : https://www.tistory.com/oauth/authorize
-
Parameter :
client_id : 등록시 발급받은 client_id
redirect_uri : 등록시 등록한 redirect_uri
response_type : "code" 라고 입력 - ex) https://www.tistory.com/oauth/authorize?client_id=abcdefghijklmnopqrstuvwxyz&redirect_uri=http://client.redirect.url&response_type=code
html 예)
<html>
<head>
<title>Tistory OAuth 2.0 JSP Sample - Example Authorization Code </TITLE>
</head>
<body>
<form method="GET" action="https://www.tistory.com/oauth/authorize/">
<input type="hidden" name="client_id" value="{발급 받은 client_id}"/>
<input type="hidden" name="redirect_uri" value="{등록시 입력한 redirect uri}"/>
<input type="hidden" name="response_type" value="code"/>
<button type="submit">Request Athorization Code</button>
</form>
</body>
</html>
03. 인증 확인 및 Authorization code 발급 (Tistory -> client)
-
1)
티스토리가 클라이언트로 부터 1번단계인 인증 요청을 받으면, 등록되어 있는 client_id와 redirect_uri의 정보가
유효한지 확인한후, 유효하다면 즉시 User Login을 유도합니다. - 2) Login 성공과 함께 유저가 연결에 동의를 하면 Authorization code를 발급하고, 다음 단계를 기다립니다.
- 3) 발급은 클라이언트 등록시 입력한 redirect_uri 로 보냅니다.
- URL : http://${redirect_uri}/?code=${authorization_code}
-
Parameter :
code : 티스토리가 발급하는 Authorization code입니다.
이 값은 클라이언트가 Access Token을 받을때 까지 잘 유지 해야합니다.
이 code는 1시간 뒤 expire 되고, 한번 사용한 값은 재사용이 불가능합니다. - ex) http://client.redirect.url?code=1234567890
- 만약 파라미터가 유효하지 않거나 여타 다른 오류가 발생할 경우 에러메세지를 redirect_uri에 실어 보냅니다.
자세한 명세는 '에러' 부분을 참조해주세요
04. 인증 허가 및 Access Token 발급 요청 단계 (client -> Tistory)
- 1) 티스토리는 최초 인증을 요청한 클라이언트가 유효한 클라이언트인지 검증을 합니다.
- 2) 발급한 code와 등록시 발급된 client_secret 이 유효한지 검증을 합니다.
- 3) 2번단계에서 받은 code 를 이 요청에 같이 실어 보내야합니다.
- URL : https://www.tistory.com/oauth/access_token
-
Parameter :
client_id : 등록시 발급 받은 client_id
client_secret : 등록시 발급 받은 client_secret
rediret_uri : 등록시 등록한 redirect_uri
code : 2번단계에서 발급받은 Authorization code
grant_type : authorization_code 라고 입력
-
ex)
https://www.tistory.com/oauth/access_token?client_id=abcdefghijklmnopqrstuvwxyz&client_secret=zxcvbnmasdfghjlk&redirect_uri=http://client.redirect.uri&
code=1234567890&grant_type=authorization_code
php 예)
<?php
$authorization_code = $_REQUEST['code'];
$client_id = '{발급받은 client_id를 입력}"';
$client_secret = '{발급받은 client_secret을 입력}';
$redirect_uri = '{등록시 입력한 redirect uri를 입력}';
$grant_type = 'authorization_code';
$url = 'https://www.tistory.com/oauth/access_token/?code=' . $authorization_code .
'&client_id=' . $client_id . '&client_secret=' . $client_secret .
'&redirect_uri=' . urlencode($redirect_uri) . '&grant_type=' . $grant_type;
$access_token = file_get_contents($url);
echo $access_token;
?>
05. Access Token 발급 (Tistory -> client)
- 1) 티스토리는 client_secret과 redirect_uri 가 유효한지 검증을 합니다.
- 2) 또한 code는 재사용되지 않았는지, expire되지 않았는지도 확인을 합니다.
- 3) 기타 모든 항목값과 일치하게 되면 Access Token이 발급이 완료됩니다.
- URL : http://${redirect_uri}/?access_token=${access_token}
- 만약 파라미터가 유효하지 않거나 여타 다른 오류가 발생할 경우 에러메세지를 redirect_uri에 실어 보냅니다.
자세한 명세는 '에러' 부분을 참조해주세요
06. Access Token 획득
- 1) 티스토리로 부터 최종적으로 Access Token 을 발급받습니다.
- 2) 클라이언트는 반드시 이 토큰을 노출의 위험이 없도록 보안에 주의를 기울여야 합니다.
07. 에러
- 1) 만약 어떠한 상황이되었든 유저인증이나 에러가 발생하면 아래와 같이 응답합니다.
- 2) 에러가 발생하면 등록된 redirect_uri로 에러메세지를 실어서 보냅니다.
- URL : http://${redirect_uri}/?error=${error}&error_reason=${error_reason}
-
Parameter :
error : OAuth 2.0 명세에 정의된 error유형들에 대한 정해진 code값들입니다.
error_reason : 해당 에러에 대한 원인이나 이유입니다.
2. Client-side flow (Javascript, Desktop app) - Implicit Grant
Javascript 등을 이용해 클라이언트 브라우저등에서만 모든 처리가 이루어지는 요청에 활용할수 있습니다.
01. Authorization 요청 단계 (client -> Tistory)
- 1) 티스토리에게 최초로 클라이언트(=컨슈머)가 인증을 요청합니다.
- 2) server-side flow와 비교하면 response_type 의 값만 다릅니다.
- 인증요청 URL : https://www.tistory.com/oauth/authorize
-
Parameter :
client_id : 등록시 발급받은 client_id
redirect_uri : 등록시 등록한 redirect_uri
response_type : "token" 이라고 입력 - ex) https://www.tistory.com/oauth/authorize?client_id=abcdefghijklmnopqrstuvwxyz&redirect_uri=http://client.redirect.url&response_type=token
html/javascript 예)
<html>
<head>
<title>Tistory OAuth 2.0 Sample - Example Implicit Grant </TITLE>
</head>
<body>
<script>
if (window.location.hash) {
alert(window.location.hash.substring(1));
}
</script>
<form method="get" action="https://www.tistory.com/oauth/authorize/">
<input type="hidden" name="client_id" value="{발급받은 client_id를 입력}"/>
<input type="hidden" name="redirect_uri" value="{등록시 입력한 redirect uri를 입력}"/>
<input type="hidden" name="response_type" value="token"/>
<button type="submit">get access_token!</button>
<form>
</body>
</html>
02. AccessToken 발급 (Tistory -> client)
티스토리는 Implicit Grant 인증요청을 받으면 유저를 로그인을 할수있도록 유도 한 후
로그인에 성공하면, URL에 #access_token= 과 expires_in를 붙혀서 다시 Redirect 시킵니다.
expires_in 는 access_token의 만료시간을 뜻하며 초단위 입니다.
- URL : http://${redirect_uri}#access_token={$accss_token}&expires_in=3600
- ex) http://client.redirect.uri#access_token=1z2x3c4v5b6n7m8z
03. AccessToken 발급 (Tistory -> client)
티스토리로 부터 최종적으로 Access Token 을 발급받습니다. 브라우저의 URL로 바로 읽어 들일 수있으며,
클라이언트는 반드시 이 토큰을 노출의 위험이 없도록 보안에 주의를 기울여야 합니다.



