๐Ÿ“Œ ์นด์นด์˜ค ๋กœ๊ทธ์ธ API ์—ฐ๋™ - 2๋‹จ๊ณ„: ์ธ๊ฐ€ ์ฝ”๋“œ๋กœ ์•ก์„ธ์Šค ํ† ํฐ ๋ฐœ๊ธ‰

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

SPRING BOOT

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

๐Ÿ“… ๋‚ ์งœ

2025-05-13

๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ

โœ… 1. ๊ฐœ์š”

  • ์ธ๊ฐ€ ์ฝ”๋“œ ์ˆ˜์‹  ํ›„ ํ† ํฐ ๋ฐœ๊ธ‰๊นŒ์ง€ ์™„๋ฃŒ๋˜์–ด์•ผ ์นด์นด์˜ค ๋กœ๊ทธ์ธ์ด ์ •์ƒ์ ์œผ๋กœ ์™„๋ฃŒ๋จ.
  • ํ† ํฐ์„ ์ด์šฉํ•ด ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ ๋ฐ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์ง.
  • application/x-www-form-urlencoded ๋ฐฉ์‹์œผ๋กœ POST ์š”์ฒญ ์ˆ˜ํ–‰.

โœ… 2. ํ† ํฐ ๋ฐœ๊ธ‰ API ์ •๋ณด

ํ•ญ๋ชฉ๋‚ด์šฉ
URLhttps://kauth.kakao.com/oauth/token
METHODPOST
์ธ์ฆ ๋ฐฉ์‹์—†์Œ (์ธ๊ฐ€ ์ฝ”๋“œ ๊ธฐ๋ฐ˜)

โœ… 3. ์š”์ฒญ ํ—ค๋” & ๋ฐ”๋”” ๊ตฌ์„ฑ

๐Ÿ“Œ ์š”์ฒญ ํ—ค๋”

Content-Type: application/x-www-form-urlencoded;charset=utf-8

๐Ÿ“Œ ์š”์ฒญ ๋ฐ”๋””

ํŒŒ๋ผ๋ฏธํ„ฐ์„ค๋ช…ํ•„์ˆ˜
grant_typeauthorization_code๋กœ ๊ณ ์ •โœ…
client_id์นด์นด์˜ค ์•ฑ์˜ REST API ํ‚คโœ…
redirect_uri๋“ฑ๋กํ•œ ๋ฆฌ๋””๋ ‰์…˜ URIโœ…
code์ธ๊ฐ€ ์ฝ”๋“œ (/callback์œผ๋กœ ์ „๋‹ฌ๋จ)โœ…

โœ… 4. ์‘๋‹ต ์˜ˆ์‹œ (JSON)

{
  "access_token": "F1BbYx6oI2MX-JgjKG5nZzEwj4Ga3F5cAAAAAQoNIZYAAAGWx0ENspQkbXeV0h_w",
  "token_type": "bearer",
  "refresh_token": "wl-wC3GxMbnS4MrinNDANVI2OVxBYEiwAAAAAgoNIZYAAAGWx0ENrJQkbXeV0h_w",
  "expires_in": 21599,
  "scope": "account_email profile_image profile_nickname",
  "refresh_token_expires_in": 5183999
}

5๏ธโƒฃ Spring Boot ์ฝ”๋“œ

// STEP 2: ์•ก์„ธ์Šค ํ† ํฐ ์š”์ฒญ ๋ฐ ์ฒ˜๋ฆฌ
@GetMapping("/callback")
public String callback(@RequestParam("code") String code){
    log.info("GET /kakao/callback..." + code);

    String url = "https://kauth.kakao.com/oauth/token";

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

    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("grant_type", "authorization_code");
    params.add("client_id", CLIENT_ID);
    params.add("redirect_uri", REDIRECT_URI);
    params.add("code", code);

    HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, header);

    RestTemplate rt = new RestTemplate();
    ResponseEntity<KakaoTokenResponse> response = rt.exchange(
        url, HttpMethod.POST, entity, KakaoTokenResponse.class
    );

    // ์‘๋‹ต ํ† ํฐ ์ €์žฅ
    this.kakaoTokenResponse = response.getBody();
    System.out.println(response);

    return "redirect:/kakao/main";
}

โœ… KakaoTokenResponse DTO

@Data
private static class KakaoTokenResponse {
    public String access_token;
    public String token_type;
    public String refresh_token;
    public int expires_in;
    public String scope;
    public int refresh_token_expires_in;
}

โœ… ๋กœ๊ทธ์ธ ํ›„ ๋ฉ”์ธ ํŽ˜์ด์ง€ ์—ฐ๊ฒฐ

@GetMapping("/main")
public void main(){
    log.info("GET /kakao/main...");
}
<!-- main.html ๋˜๋Š” main.jsp -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>๋ฉ”์ธ ํŽ˜์ด์ง€</h1>
</body>
</html>

๐Ÿ”ฅ ์ •๋ฆฌ

  • ์ธ๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋ฐ›์€ ํ›„ ํ† ํฐ ๋ฐœ๊ธ‰๊นŒ์ง€ ์™„๋ฃŒํ•ด์•ผ ์นด์นด์˜ค ๋กœ๊ทธ์ธ์ด ์ •์ƒ ์ฒ˜๋ฆฌ๋œ๋‹ค.
  • ์‘๋‹ต JSON์€ DTO๋กœ ๋งคํ•‘ํ•˜์—ฌ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ† ํฐ์„ ๋ฐ›์•„์™”๋‹ค๋ฉด ์ด์ œ ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ์„ ํ†ตํ•ด ์‹ค์ œ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉ ๊ฐ€๋Šฅ.

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


profile
Here, My Pale Blue.๐ŸŒ

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