Facebook OAuth를 통한 인증 처리 - Mimul's Developer World

Facebook OAuth를 통한 인증 처리 - Mimul's Developer World:


Facebook OAuth를 통한 인증 처리

이번에는 Open Graph의 OAuth를 통해 Facebook의 이용자 인증 처리 하는 로직을 테스트한 내용을 공유합니다.

1. 개요
 - Facebook은 FB Connect라는 API 와 Open Graph의 OAuth를 통해 Facebook의 이용자 인증 처리를 대행할 수 있다.
 - FB Connect 보다 OAuth를 통해 Facebook의 이용자 인증 처리가 도 안정적이라는 설(?)이 있다. 믿거나 말거나 ^^

2. 관련 표준 설명(OAuth 1.0a 와는 다른 OAuth 2.0을 적용함 - 트위터와는 또 다름)
 - 참조 사이트 : http://developers.facebook.com/docs/authentication/
 - 사전 작업(Facebook 어플 등록을 통해 키 발급)
   -> 등록 사이트 : http://www.facebook.com/developers/createapp.php
   -> 확인 정보 : application id, api_key, application secret
 - code값 요청(apps에대한 검증 코드 값)
   -> 요청 정보 : https://graph.facebook.com/oauth/authorize? client_id="등록한 application id"&redirect_uri="code 값을 받을 callback url"&scope=publish_stream,offline_access,user_about_me,read_friendlists
   -> 응답 정보 : code 값이 넘어옴.
   -> redirect_uri 는 app 등록할 때에 입력했던 url과 domain이 같아야함. 안그러면 error남
   -> scope는 허가 받을 사용자의 정보 종류
 - code 값을 통한 session key 요청
   -> 요청 정보 :  https://graph.facebook.com/oauth/access_token? client_id=""&redirect_uri=""&client_secret="등록한 application secret"&code="code 값 요청을 통해 return 받은 값"
   -> 응답 정보 : access_token값이 넘어옴.
   -> 이 access_token값을 가지고 facebook 고객 정보, Stream 정보 등 scope 을 가져올 수 있음

3. 관련 샘플 소스
 - 참조 오픈 소스 :  RestFB - http://restfb.com/(access_token을 가지고 개인 정보 등을 가져올 수 있음)
 - 샘플 소스
public void service(RequestContext rc) throws IOException {
    code = StringUtils.defaultIfEmpty(request.getParameter("code"), "");
    if (StringUtils.isEmpty(code)) {
     loginURL = getLoginRedirectURL();
     response.sendRedirect(loginURL);
     return;
    } else {
     authURL = getAuthURL(code);
     connector = SimpleHttpConnector.getDefault();
     r = connector.doGET(authURL, (Map<?,?>)null);
     if (r != null && r.getStatus() == 200) {
      accessToken = readUrl(r.getInputStream()).split("&")[0]
      .replaceFirst("access_token=", "");
      log.warn("access_token=" + accessToken);
      }
}
          
public static String getLoginRedirectURL() {
 String login = null;
 try {
  login = "https://graph.facebook.com/oauth/authorize?
                           client_id=" +
      client_id + "&display=page&redirect_uri=" + 
      URLEncoder.encode(redirect_uri, "utf-8") + "&scope=" + 
      ArrayUtil.delimitObjectsToString(",", perms);
 } catch (Exception e) {
  e.printStackTrace();
  log.error(e);
 }
 return login;
}

public static String getAuthURL(String authCode) {
 String auth = null;
 try {
  auth = "https://graph.facebook.com/oauth/access_token?
                           client_id=" +
      client_id + "&redirect_uri=" + URLEncoder.encode(redirect_uri, "utf-8") + 
      "&client_secret=" + 
      secret + "&code=" + authCode;
 } catch (Exception e) {
  e.printStackTrace();
  log.error(e);
 }
 return auth;
}

protected String readUrl(InputStream is) throws IOException 
{
 StringBuffer response = null;
 BufferedReader in = null;
 String inputLine = null;
 
 try {
  if (is == null)
   return "";
  response = new StringBuffer(300);
  in = new BufferedReader(new InputStreamReader(is));
  while ((inputLine = in.readLine()) != null)
   response.append(inputLine);
  log.error("readUrl:" + response.toString());
 } catch (Exception e) {
  e.printStackTrace();
  log.error(e);
 } finally {
  if (in != null)
   in.close();
 }
 return response.toString();
}

4. 데모 사이트
 - http://mimul.com/rpx/oauth

[관련 포스트]

[참조 사이트]

댓글

이 블로그의 인기 게시물

4,5,6 띠 저항의 색띠를 읽는 법(띠저항 값)

수지에서 인천공항 리무진 버스 (인천공항버스정보)(2022년3월업데이트)

수지에서 김포공항 리무진 버스 ( 2022년 3월 업데이트 )