-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joo
committed
Jan 5, 2017
0 parents
commit 4f033fb
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<%@ page contentType="text/html;charset=utf-8"%> | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | ||
<html> | ||
<head> | ||
<title>Tistory OAuth 2.0 JSP Sample - Example Authorization Code </TITLE> | ||
<style> | ||
.form { text-align:center; padding: 100px } | ||
.btn { padding:20px; font-size:24px } | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<form class="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"/> <!-- 이단계에서는 "code" 라고 반드시 입력 --> | ||
<button class="btn" type="submit">Request Athorization Code</button> | ||
</form> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<%@ page contentType="text/html;charset=utf-8"%> | ||
|
||
<%@ page import="java.io.BufferedReader"%> | ||
<%@ page import="java.io.InputStream"%> | ||
<%@ page import="java.io.InputStreamReader"%> | ||
<%@ page import="java.net.URL"%> | ||
<%@ page import="java.net.URLConnection"%> | ||
<%@ page import="java.security.Security;"%> | ||
|
||
<% | ||
/* | ||
* OAuth2.0 단계중 Authorization code callback 및 Access Token 발급의 간단 예제 | ||
*/ | ||
String authorization_code = request.getParameter("code"); //최초 발급 요청으로 부터 받은 authorization code 입력 | ||
String client_id = "{발급받은 client_id를 입력하세요}"; | ||
String client_secret = "{발급받은 client_secret 을 입력하세요}"; | ||
String redirect_uri = "{등록시 입력한 redirect uri 를 입력하세요}"; | ||
String grant_type = "authorization_code"; //반드시 이단계에서는 authorization_code 라고 입력 | ||
String url = "https://www.tistory.com/oauth/access_token/?code=" + authorization_code + | ||
"&client_id=" + client_id + | ||
"&client_secret=" + client_secret + | ||
"&redirect_uri=" + redirect_uri + | ||
"&grant_type=" + grant_type; | ||
try { | ||
System.setProperty ( "java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); | ||
com.sun.net.ssl.internal.ssl.Provider provider = new com.sun.net.ssl.internal.ssl.Provider(); | ||
Security.addProvider(provider); | ||
URLConnection connection; | ||
InputStream is; | ||
InputStreamReader isr; | ||
BufferedReader br; | ||
URL url = new URL(url); | ||
connection = url.openConnection(); | ||
is = connection.getInputStream(); | ||
isr = new InputStreamReader(is); | ||
br = new BufferedReader(isr); | ||
while (true) { | ||
if (br.readLine() == null) | ||
break; | ||
out.println(br.readLine()); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
%> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 티스토리 OAuth2 JSP/Servlet sample code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | ||
<html> | ||
<head> | ||
<title>Tistory OAuth 2.0 JSP Sample - Example Authorization Code </TITLE> | ||
<style> | ||
.form { text-align:center; padding: 100px } | ||
.btn { padding:20px; font-size:24px } | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<form class="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"/> <!-- 이단계에서는 "code" 라고 반드시 입력 --> | ||
<button class="btn" type="submit">Request Athorization Code</button> | ||
</form> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package oauth; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.security.Security; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
|
||
@SuppressWarnings("serial") | ||
public class CallbackAndRequestAccessToken extends HttpServlet { | ||
|
||
protected void processRequest(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
response.setContentType("text/html;charset=UTF-8"); | ||
PrintWriter out = response.getWriter(); | ||
|
||
try { | ||
|
||
/** | ||
* OAuth2.0 단계중 Authorization code callback 및 Access Token 발급의 간단 예제 | ||
*/ | ||
|
||
String authorization_code = request.getParameter("code"); //최초 발급 요청으로 부터 받은 authorization code 입력 | ||
|
||
String clientId = "{발급받은 client_id를 입력하세요}"; | ||
String clientSecret = "{발급받은 client_secret 을 입력하세요}"; | ||
String redirectUri = "{등록시 입력한 redirect uri 를 입력하세요}"; | ||
String grantType = "authorization_code"; | ||
|
||
String requestUrl = "https://www.tistory.com/oauth/access_token/?code=" + authorization_code + | ||
"&client_id=" + clientId + | ||
"&client_secret=" + clientSecret + | ||
"&redirect_uri=" + redirectUri + | ||
"&grant_type=" + grantType; | ||
|
||
try { | ||
|
||
System.setProperty ( "java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); | ||
com.sun.net.ssl.internal.ssl.Provider provider = new com.sun.net.ssl.internal.ssl.Provider(); | ||
Security.addProvider(provider); | ||
|
||
URL url = new URL(requestUrl); | ||
URLConnection connection = url.openConnection(); | ||
|
||
InputStream is = connection.getInputStream(); | ||
InputStreamReader isr = new InputStreamReader(is); | ||
BufferedReader br = new BufferedReader(isr); | ||
|
||
out.println(br.readLine()); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} finally { | ||
out.close(); | ||
} | ||
} | ||
|
||
protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
processRequest(request, response); | ||
} | ||
|
||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
processRequest(request, response); | ||
} | ||
|
||
public String getServletInfo() { | ||
return "Short description"; | ||
} | ||
|
||
} |