SPRING
2022.05.02 / 16:06

Spring MVC Ä«Ä«¿À ¾ÆÀ̵ð·Î ·Î±×ÀÎ(REST API)

ÄÚÄÚ·Î
Ãßõ ¼ö 116

Spring MVC Ä«Ä«¿À ¾ÆÀ̵ð·Î ·Î±×ÀÎ(REST API)

FIF 2019. 6. 28. 18:33

https://developers.kakao.com/

 

Kakao Developers_

´õ ³ªÀº ¼¼»óÀ» ²Þ²Ù°í ±×°ÍÀ» Çö½Ç·Î ¸¸µå´Â À̸¦ À§ÇÏ¿© Ä«Ä«¿À¿¡¼­ ¾Û °³¹ß Ç÷§Æû ¼­ºñ½º¸¦ ½ÃÀÛÇÕ´Ï´Ù.

developers.kakao.com

 Ä«Ä«¿Àµðº§·ÎÆÛ »çÀÌÆ® µé¾î°£ ÈÄ ·Î±×ÀÎ

 

 

 

Ä«Ä«¿À°èÁ¤ ·Î±×ÀΠŬ¸¯

 

 

¾Û °³¹ß ½ÃÀÛÇϱâ Ŭ¸¯

 

 

 

ÁÂÃø¿¡ ¾Û¸¸µé±â Ŭ¸¯

 

 

¾Û À̸§°ú ȸ»ç¸í ¼³Á¤ ÈÄ, 

 

°è¼Ó ÁøÇà Ŭ¸¯

 

 

¾ÛÀ» ¸¸µç ÈÄ, REST APIÅ°¸¦ ¸Þ¸ðÀå°°Àº°÷¿¡ º¹»çÇØ µÎÀÚ

 

 

 

ÁÂÃø¿¡ ÀڱⰡ ¸¸µç ¾îÇø®ÄÉÀ̼ǿ¡ µé¾î°£´Ù.

 

 

ÀÌ·±½ÄÀ¸·Î »ç¿ëºÒ°¡°¡ ¶ãÅÙµ¥, »ç¿ëÀÚ °ü¸®¿¡ µé¾î°¡¼­ ±âº» ¼³Á¤À» ÇØÁÖ¾î¾ß ÇÑ´Ù.

 

 

OFFµÅÀÖ´Â°É Å¬¸¯Çؼ­ ONÀ¸·Î ¹Ù²ÙÀÚ.

 

 

 

ÇÊ¿äÇÑ Ç׸ñ üũÇÏ°í, ÀúÀåÀ» ´©¸¥´Ù. ¼öÁý¸ñÀûÀº Àû´çÇÏ°Ô ½áÁØ´Ù.

 

 

 

¾Û Á¤º¸ ¼³Á¤À» ´©¸¥´Ù.

 

 

À¥ ¼³Á¤À» ´©¸¥ÈÄ, »çÀÌÆ® µµ¹Ì¿£°ú Redirect Path¸¦ ¼³Á¤ÇÑ´Ù.

À̰͵µ ¸Þ¸ðÀå¿¡ Àû¾îµÎÀÚ.

ÄÚµùÇÒ¶§ ¾²À̴ϱî.

 

 

 

  private final static String K_CLIENT_ID = "ÀڱⲨ REST APIÅ° º¹ºÙ";
                                              //ÀÌ·±½ÄÀ¸·Î REDIRECT_URI¸¦ ½á³Ö´Â´Ù.                                                                                                  //                                                //
   private final static String K_REDIRECT_URI = "http://localhost:8080/myfinal/kakaologin.do";

   public static String getAuthorizationUrl(HttpSession session) {
      String kakaoUrl = "https://kauth.kakao.com/oauth/authorize?" + "client_id=" + K_CLIENT_ID + "&redirect_uri="
            + K_REDIRECT_URI + "&response_type=code";
      return kakaoUrl;
   }


public static JsonNode getAccessToken(String autorize_code) {
      final String RequestUrl = "https://kauth.kakao.com/oauth/token";
      final List<NameValuePair> postParams = new ArrayList<NameValuePair>();
      postParams.add(new BasicNameValuePair("grant_type", "authorization_code"));
      postParams.add(new BasicNameValuePair("client_id", "ÀڱⲨREST API KEYº¹ºÙ")); // REST API KEY
      postParams.add(new BasicNameValuePair("redirect_uri", "http://localhost:8080/myfinal/kakaologin.do")); // ¸®´ÙÀÌ·ºÆ® URI                                                              
      postParams.add(new BasicNameValuePair("code", autorize_code)); // ·Î±×ÀÎ °úÁ¤Áß ¾òÀº code °ª
      final HttpClient client = HttpClientBuilder.create().build();
      final HttpPost post = new HttpPost(RequestUrl);
      JsonNode returnNode = null;
      try {
         post.setEntity(new UrlEncodedFormEntity(postParams));
         final HttpResponse response = client.execute(post);
         // JSON ÇüÅ ¹Ýȯ°ª ó¸®
         ObjectMapper mapper = new ObjectMapper();
         returnNode = mapper.readTree(response.getEntity().getContent());
      } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
      } catch (ClientProtocolException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         // clear resources
      }
      return returnNode;
   }
   
   public static JsonNode getKakaoUserInfo(JsonNode accessToken) {
      final String RequestUrl = "https://kapi.kakao.com/v2/user/me";
      final HttpClient client = HttpClientBuilder.create().build();
      final HttpPost post = new HttpPost(RequestUrl);
      // add header
      post.addHeader("Authorization", "Bearer " + accessToken);
      JsonNode returnNode = null;
      try {
         final HttpResponse response = client.execute(post);
         // JSON ÇüÅ ¹Ýȯ°ª ó¸®
         ObjectMapper mapper = new ObjectMapper();
         returnNode = mapper.readTree(response.getEntity().getContent());
      } catch (ClientProtocolException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         // clear resources
      }
      return returnNode;
   }

KakaoController¶ó´Â À̸§À¸·Î Ŭ·¡½º¸¦ Çϳª¸¸µé°í, ÀڱⲨ REST APIÅ°¸¦ ÇØ´çÇÏ´Â °÷¿¡ ºÙ¿©³Ö±â ÇÑ´Ù.

 

 

 

³ª´Â ÀÌ·±½ÄÀ¸·Î ·Î±×ÀÎâÀ» ±¸ÇöÇß´Ù.

 

        <div id="kakao_id_login" style="text-align: center">
                     <a href="${kakao_url}">
                     <img width="223" 
                        src="images/kakao_account_login_btn_medium_narrow.png" /></a>
        </div>

Ä«Ä«¿À°èÁ¤À¸·Î ·Î±×ÀÎ ÂÊ VIEW¼Ò½º´Â ÀÌ·¸´Ù.

·Î±×ÀÎ ¹öÆ°Àº ±¸±Û¸µÇؼ­ ´Ù¿î¹ÞÀÚ. 

ÀÚ, ±×·³ ÀÌ kakao_urlÀº ¾îµð¼­ ¹Þ¾Æ¿À´Â °Å³Ä?

 

 

@RequestMapping(value = "/memberloginform.do", method = RequestMethod.GET)
	public ModelAndView memberLoginForm(HttpSession session) {
		ModelAndView mav = new ModelAndView();
		/* ³×¾Æ·Î ÀÎÁõ URLÀ» »ý¼ºÇϱâ À§ÇÏ¿© getAuthorizationUrlÀ» È£Ãâ */
		String naverAuthUrl = naverLoginDTO.getAuthorizationUrl(session);
		String kakaoUrl = KakaoController.getAuthorizationUrl(session);

		/* »ý¼ºÇÑ ÀÎÁõ URLÀ» View·Î Àü´Þ */
		mav.setViewName("memberloginform");
		// ³×À̹ö ·Î±×ÀÎ
		mav.addObject("naver_url", naverAuthUrl);
		// Ä«Ä«¿À ·Î±×ÀÎ
		mav.addObject("kakao_url", kakaoUrl);

		return mav;
	}// end memberLoginForm()

MainController.java ÆÄÀÏÀε¥, 

 

String kakaoUrl = KakaoController.getAuthorizationUrl(session);

mav.addObject("kakao_url", kakaoUrl);

 

ÀÌ ºÎºÐ¿¡¼­ kakao_urlÀ» ¹Þ¾Æ¿Â´Ù.

ModelAndView·Î ¼³Á¤ÇßÀ¸¹Ç·Î, ¸¶Áö¸·¿¡ return mavÇϸé kakao_urlÀÌ ¹ÝȯµÇ¼­

view´Ü¿¡¼­ ¹ÞÀ» ¼ö ÀÖ´Â °ÍÀÌ´Ù.

 

 

@RequestMapping(value = "/kakaologin.do", produces = "application/json", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView kakaoLogin(@RequestParam("code") String code, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		ModelAndView mav = new ModelAndView();
		// °á°ú°ªÀ» node¿¡ ´ã¾ÆÁÜ
		JsonNode node = KakaoController.getAccessToken(code);
		// accessToken¿¡ »ç¿ëÀÚÀÇ ·Î±×ÀÎÇÑ ¸ðµç Á¤º¸°¡ µé¾îÀÖÀ½
		JsonNode accessToken = node.get("access_token");
		// »ç¿ëÀÚÀÇ Á¤º¸
		JsonNode userInfo = KakaoController.getKakaoUserInfo(accessToken);
		String kemail = null;
		String kname = null;
		String kgender = null;
		String kbirthday = null;
		String kage = null;
		String kimage = null;
		// À¯ÀúÁ¤º¸ Ä«Ä«¿À¿¡¼­ °¡Á®¿À±â Get properties
		JsonNode properties = userInfo.path("properties");
		JsonNode kakao_account = userInfo.path("kakao_account");
		kemail = kakao_account.path("email").asText();
		kname = properties.path("nickname").asText();
		kimage = properties.path("profile_image").asText();
		kgender = kakao_account.path("gender").asText();
		kbirthday = kakao_account.path("birthday").asText();
		kage = kakao_account.path("age_range").asText();
		session.setAttribute("kemail", kemail);
		session.setAttribute("kname", kname);
		session.setAttribute("kimage", kimage);
		session.setAttribute("kgender", kgender);
		session.setAttribute("kbirthday", kbirthday);
		session.setAttribute("kage", kage);
		mav.setViewName("main");
		return mav;
	}// end kakaoLogin()

value="/kakaologin.do"´Â À§¿¡¼­ ¼³Á¤Çß´ø

¿©±â¿Í °°¾Æ¾ß ÇÑ´Ù.

 



Ãâó: https://thefif19wlsvy.tistory.com/62 [FIF's ÄÚµùÆÑÅ丮]