[OAUTH2] 페이스북(facebook) OAuthCallbackListener 샘플
IT/기타2020. 10. 14. 16:18
- Access Token 요청 (graph.facebook.com/oauth/access_token)
- 사용자 게시물 피드 조회 : /{user-id}/feed (graph.facebook.com/v2.5/me/feed)
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
package com.example; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
import java.net.URLEncoder; | |
import java.nio.charset.StandardCharsets; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.commons.codec.binary.Base64; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.util.EntityUtils; | |
import org.json.JSONObject; | |
public class OAuthCallbackListener extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
// Detect the presence of an authorization code | |
String authorizationCode = request.getParameter("code"); | |
if (authorizationCode != null && authorizationCode.length() > 0) { | |
final String TOKEN_ENDPOINT = "https://graph.facebook.com/oauth/access_token"; | |
final String GRANT_TYPE = "authorization_code"; | |
final String REDIRECT_URI = "https://www.test.com:8443/callback"; | |
final String CLIENT_ID = "CLIENT_ID"; | |
final String CLIENT_SECRET = "CLIENT_SECRET"; | |
HttpPost httpPost = new HttpPost(TOKEN_ENDPOINT | |
+ "?grant_type=" + URLEncoder.encode(GRANT_TYPE, StandardCharsets.UTF_8.name()) | |
+ "&code=" + URLEncoder.encode(authorizationCode, StandardCharsets.UTF_8.name()) | |
+ "&redirect_uri=" + URLEncoder.encode(REDIRECT_URI, StandardCharsets.UTF_8.name()) | |
+ "&client_id=" + URLEncoder.encode(CLIENT_ID, StandardCharsets.UTF_8.name())); | |
String clientCredentials = CLIENT_ID + ":" + CLIENT_SECRET; | |
String encodedClientCredentials = new String(Base64.encodeBase64(clientCredentials.getBytes())); | |
httpPost.setHeader("Authorization", "Basic " + encodedClientCredentials); | |
CloseableHttpClient httpClient = HttpClients.createDefault(); | |
HttpResponse httpResponse = httpClient.execute(httpPost); | |
HttpEntity entity = httpResponse.getEntity(); | |
String line = EntityUtils.toString(entity, "UTF-8"); | |
JSONObject jObject = new JSONObject(line); | |
String accessToken = jObject.getString("access_token"); | |
System.out.println("accessToken: " + accessToken); | |
String requestUrl = "https://graph.facebook.com/v2.5/me/feed?limit=25"; | |
httpClient = HttpClients.createDefault(); | |
httpPost = new HttpPost(requestUrl); | |
httpPost.addHeader("Authorization", "Bearer " + accessToken); | |
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); | |
urlParameters.add(new BasicNameValuePair("method", "get")); | |
httpPost.setEntity(new UrlEncodedFormEntity(urlParameters)); | |
httpResponse = httpClient.execute(httpPost); | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); | |
String feedJson = bufferedReader.readLine(); | |
System.out.println("data: " + feedJson); | |
httpClient.close(); | |
} else { | |
} | |
} | |
} |
'IT > 기타' 카테고리의 다른 글
인증서 만료일자 조회 (0) | 2022.05.19 |
---|---|
[SAML Test] 4. Service Provider 생성 및 테스트 (0) | 2020.09.07 |
[SAML Test] 3. Setup our Identity Provider (Docker환경에 설치) (0) | 2020.09.07 |
[SAML Test] 1. Setup a Single Sign On SAML Test Environment with Docker and NodeJS (0) | 2020.09.07 |
[SAML Test] 2. CentOS 8 nvm을 이용한 node.js 설치 (0) | 2020.09.07 |