๐Ÿ“Œ ์นด์นด์˜ค ๋กœ๊ทธ์ธ API ์—ฐ๋™ - 3๋‹จ๊ณ„: ์•ก์„ธ์Šค ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ

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

SPRING BOOT

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

๐Ÿ“… ๋‚ ์งœ

2025-05-13


๐Ÿ” ์ „์ฒด ๋กœ๊ทธ์ธ ํ๋ฆ„ ์š”์•ฝ

[์‚ฌ์šฉ์ž โ†’ ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์š”์ฒญ]
      โ†“
[์ธ๊ฐ€ ์ฝ”๋“œ ๋ฐœ๊ธ‰] (1๋‹จ๊ณ„)
      โ†“
[์ธ๊ฐ€ ์ฝ”๋“œ โ†’ ์•ก์„ธ์Šค ํ† ํฐ ์š”์ฒญ] (2๋‹จ๊ณ„)
      โ†“
[์•ก์„ธ์Šค ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ] โ† ์ง€๊ธˆ ๋‹จ๊ณ„! (3๋‹จ๊ณ„)
      โ†“
[์‚ฌ์šฉ์ž ์ •๋ณด๋กœ ํšŒ์›๊ฐ€์ž…/๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ]

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

โœ… 1. ๊ฐœ์š”

  • ์•ก์„ธ์Šค ํ† ํฐ์„ ์ด์šฉํ•ด ์นด์นด์˜ค ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์š”์ฒญํ•จ
  • ์‚ฌ์šฉ์ž ์ •๋ณด๋Š” id, nickname, email, profile_image ๋“ฑ
  • ์‘๋‹ต ๊ฒฐ๊ณผ๋Š” DTO๋กœ ๋งคํ•‘ํ•˜์—ฌ Thymeleaf ๋“ฑ ํ…œํ”Œ๋ฆฟ์— ์ „๋‹ฌ ๊ฐ€๋Šฅ

โœ… 2. API ์š”์ฒญ ์ •๋ณด

ํ•ญ๋ชฉ๊ฐ’
์š”์ฒญ URLhttps://kapi.kakao.com/v2/user/me
METHODPOST ๋˜๋Š” GET (POST ๊ถŒ์žฅ)
์ธ์ฆ ๋ฐฉ์‹Authorization: Bearer {ACCESS_TOKEN}

โœ… 3. ์š”์ฒญ ํ—ค๋”

ํ—ค๋”์„ค๋ช…ํ•„์ˆ˜
Authorization์•ก์„ธ์Šค ํ† ํฐ (Bearer ...)โœ…
Content-Typeapplication/x-www-form-urlencoded;charset=utf-8โœ…

4๏ธโƒฃ ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ ์ฝ”๋“œ (Spring Boot)

private KakaoProfileResponse kakaoProfileResponse;

@GetMapping("/main")
public void main(Model model) {
    log.info("GET /kakao/main...");

    String url = "https://kapi.kakao.com/v2/user/me";

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

    HttpEntity entity = new HttpEntity<>(header);

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

    this.kakaoProfileResponse = response.getBody();
    model.addAttribute("profile", this.kakaoProfileResponse);

    System.out.println(this.kakaoProfileResponse);
}

โœ… KakaoProfileResponse DTO

@Data
private static class KakaoProfileResponse {
    public long id;
    public Date connected_at;
    public Properties properties;
    public KakaoAccount kakao_account;
}

@Data
private static class Properties {
    public String nickname;
    public String profile_image;
    public String thumbnail_image;
}

@Data
private static class KakaoAccount {
    public boolean profile_nickname_needs_agreement;
    public boolean profile_image_needs_agreement;
    public Profile profile;
    public boolean has_email;
    public boolean email_needs_agreement;
    public boolean is_email_valid;
    public boolean is_email_verified;
    public String email;
}

@Data
private static class Profile {
    public String nickname;
    public String thumbnail_image_url;
    public String profile_image_url;
    public boolean is_default_image;
    public boolean is_default_nickname;
}

๐Ÿ–ฅ๏ธ ์‚ฌ์šฉ์ž ์ •๋ณด ์ถœ๋ ฅ ๋ทฐ (Thymeleaf)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>์นด์นด์˜ค ๋กœ๊ทธ์ธ ์„ฑ๊ณต</title>
</head>
<body>
<h1>๋ฉ”์ธ ํŽ˜์ด์ง€</h1>
<hr/>

<!-- ์ „์ฒด ์‘๋‹ต DTO ์ถœ๋ ฅ -->
<div th:text="${profile}"></div>

<!-- ์„ ํƒ์ : ๊ฐœ๋ณ„ ์ •๋ณด ์ถœ๋ ฅ -->
<!-- <p th:text="${profile.kakao_account.email}"></p> -->
<!-- <img th:src="${profile.kakao_account.profile.profile_image_url}" /> -->
</body>
</html>

๐Ÿ”ฅ ์ •๋ฆฌ

  • ์•ก์„ธ์Šค ํ† ํฐ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ์„ ์ˆ˜ํ–‰
  • ์‘๋‹ต์€ DTO๋กœ ๋งคํ•‘ํ•˜์—ฌ Java ๊ฐ์ฒด๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
  • ๋กœ๊ทธ์ธ ํ›„ ์‚ฌ์šฉ์ž์˜ ์ด๋ฉ”์ผ, ๋‹‰๋„ค์ž„, ํ”„๋กœํ•„ ๋“ฑ์„ ์›น์— ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Œ

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


profile
Here, My Pale Blue.๐ŸŒ

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