๐Ÿ“Œ ์นด์นด์˜คํ†ก ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ API ์—ฐ๋™ (Spring Boot)

My Pale Blue Dotยท2025๋…„ 5์›” 13์ผ
0

SPRING BOOT

๋ชฉ๋ก ๋ณด๊ธฐ
22/40
post-thumbnail

๐Ÿ“… 2025-05-13


๐Ÿ“ ํ•™์Šต ๋ชฉํ‘œ

  • ์นด์นด์˜ค ๋””๋ฒจ๋กœํผ์Šค์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์„ค์ •ํ•˜๊ณ 
  • OAuth2 ์ธ์ฆ์„ ํ†ตํ•ด access token์„ ๋ฐœ๊ธ‰๋ฐ›์€ ํ›„
  • RestTemplate์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž์‹ ์—๊ฒŒ ์นด์นด์˜คํ†ก ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” API๋ฅผ ๊ตฌํ˜„

1๏ธโƒฃ ์นด์นด์˜ค ๋””๋ฒจ๋กœํผ์Šค ์„ค์ •

โœ… 1. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก

  1. https://developers.kakao.com ์ ‘์†
  2. ๋กœ๊ทธ์ธ ํ›„ [๋‚ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜] > ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ถ”๊ฐ€
    • ์•ฑ ์ด๋ฆ„, ์‚ฌ์—…์ž ์—ฌ๋ถ€ ๋“ฑ๋ก

โœ… 2. ํ”Œ๋žซํผ ๋“ฑ๋ก

  • [๋‚ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜] โ†’ [ํ”Œ๋žซํผ] ํƒญ โ†’ Web ์„ ํƒ
  • ์‚ฌ์ดํŠธ ๋„๋ฉ”์ธ ๋“ฑ๋ก:
http://localhost:8090
http://192.168.16.11:8090

โœ… 3. Redirect URI ๋“ฑ๋ก

  • [์นด์นด์˜ค ๋กœ๊ทธ์ธ] โ†’ Redirect URI ๋“ฑ๋ก
  • ์˜ˆ์‹œ:
http://localhost:8090/kakao/callback
http://192.168.16.11:8090/kakao/callback

โœ… 4. ์นด์นด์˜ค ๋กœ๊ทธ์ธ ํ™œ์„ฑํ™”

  • [์นด์นด์˜ค ๋กœ๊ทธ์ธ] ํƒญ > ํ™œ์„ฑํ™”
  • ๋กœ๊ทธ์ธ ON โ†’ ๊ธฐ๋ณธ ๋™์˜ ํ•ญ๋ชฉ ์„ค์ • ๊ฐ€๋Šฅ

โœ… 5. ๋™์˜ ํ•ญ๋ชฉ ์„ค์ •

  • [์นด์นด์˜ค ๋กœ๊ทธ์ธ > ๋™์˜ํ•ญ๋ชฉ] ํƒญ โ†’ ํ•ญ๋ชฉ ์ถ”๊ฐ€
ํ•ญ๋ชฉ ์ด๋ฆ„ID์„ค์ •
์นด์นด์˜คํ†ก ๋ฉ”์‹œ์ง€ ์ „์†กtalk_message์ด์šฉ ์ค‘์œผ๋กœ ์„ค์ •

์ด ํ•ญ๋ชฉ์ด ์žˆ์–ด์•ผ ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ API ํ˜ธ์ถœ ๊ฐ€๋Šฅ


2๏ธโƒฃ ์ธ๊ฐ€ ์ฝ”๋“œ ์š”์ฒญ URL (์Šค์ฝ”ํ”„ ํฌํ•จ)

@GetMapping("/getCodeMsg")
public String getCode_message(){
    return "redirect:https://kauth.kakao.com/oauth/authorize"
        + "?client_id=" + CLIENT_ID
        + "&redirect_uri=" + REDIRECT_URI
        + "&response_type=code"
        + "&scope=talk_message";  // โœ… ๋ฉ”์‹œ์ง€ ์ „์†ก ๊ถŒํ•œ ์š”์ฒญ
}
  • ์ธ๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋ฐ›์€ ํ›„, ์„œ๋ฒ„์—์„œ access_token์„ ๋ฐœ๊ธ‰๋ฐ›์•„ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ API (Spring Boot)

@GetMapping("/message/me/{message}")
public void message_me(@PathVariable("message") String message){
    String url = "https://kapi.kakao.com/v2/api/talk/memo/default/send";

    HttpHeaders header = new HttpHeaders();
    header.add("Authorization", "Bearer " + kakaoTokenResponse.getAccess_token());
    header.add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

    JSONObject template_object = new JSONObject();
    template_object.put("object_type", "text");
    template_object.put("text", message);
    template_object.put("link", new JSONObject()); // ํ•„์ˆ˜ ํ•„๋“œ
    template_object.put("button_title", "");       // ๋ฒ„ํŠผ ์—†์ด ๊ฐ€๋Šฅ

    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("template_object", template_object.toString());

    HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, header);
    new RestTemplate().exchange(url, HttpMethod.POST, entity, String.class);
}

๐Ÿ” ํ…Œ์ŠคํŠธ ํ๋ฆ„ ์š”์•ฝ

  1. ๋ธŒ๋ผ์šฐ์ € ์ ‘์† โ€“ ์ธ๊ฐ€ ์ฝ”๋“œ ์š”์ฒญ

    http://localhost:8090/kakao/getCodeMsg
    
  2. ์‚ฌ์šฉ์ž ๋™์˜ ํ›„ Redirect URI๋กœ ์ธ๊ฐ€ ์ฝ”๋“œ ์ „๋‹ฌ

  3. ์ธ๊ฐ€ ์ฝ”๋“œ๋กœ AccessToken ๋ฐœ๊ธ‰

  4. ๋ฉ”์‹œ์ง€ ์ „์†ก

    http://localhost:8090/kakao/message/me/์•ˆ๋…•ํ•˜์„ธ์š”
    
  5. ์นด์นด์˜คํ†ก ์•ฑ์˜ โ€œ๋‚˜์™€์˜ ์ฑ„ํŒ…๋ฐฉโ€์—์„œ ๋ฉ”์‹œ์ง€ ํ™•์ธ


๐Ÿ”ฅ ์‹ค๋ฌด ์ •๋ฆฌ ํฌ์ธํŠธ

ํ•ญ๋ชฉ์„ค๋ช…
talk_messageํ•„์ˆ˜ ์Šค์ฝ”ํ”„. ์‚ฌ์šฉ์ž ๋™์˜ ํ•„์š”
template_object๋ฉ”์‹œ์ง€ ๋‚ด์šฉ ๊ตฌ์„ฑ. ๋ฐ˜๋“œ์‹œ JSON ํ˜•์‹
link, button_title์ƒ๋žต ๋ถˆ๊ฐ€์ง€๋งŒ ๊ฐ’์€ ๋น„์›Œ๋„ ๋จ
์ธ์ฆ ํ๋ฆ„์ธ๊ฐ€ ์ฝ”๋“œ โ†’ AccessToken โ†’ ๋ฉ”์‹œ์ง€ ์ „์†ก
ํ† ํฐ ์ €์žฅ๋ณด์•ˆ์„ ์œ„ํ•ด ์„ธ์…˜ ๋˜๋Š” DB์— ์ €์žฅ ๊ถŒ์žฅ

๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ


profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€