๐
๋ ์ง
2025-05-13
๐ ์ ์ฒด ๋ก๊ทธ์ธ ํ๋ฆ ์์ฝ
[์ฌ์ฉ์ โ ์นด์นด์ค ๋ก๊ทธ์ธ ์์ฒญ]
โ
[์ธ๊ฐ ์ฝ๋ ๋ฐ๊ธ] (1๋จ๊ณ)
โ
[์ธ๊ฐ ์ฝ๋ โ ์ก์ธ์ค ํ ํฐ ์์ฒญ] (2๋จ๊ณ)
โ
[์ก์ธ์ค ํ ํฐ์ผ๋ก ์ฌ์ฉ์ ์ ๋ณด ์์ฒญ] โ ์ง๊ธ ๋จ๊ณ! (3๋จ๊ณ)
โ
[์ฌ์ฉ์ ์ ๋ณด๋ก ํ์๊ฐ์
/๋ก๊ทธ์ธ ์ฒ๋ฆฌ]
๐ ํ์ต ๋ด์ฉ
โ
1. ๊ฐ์
- ์ก์ธ์ค ํ ํฐ์ ์ด์ฉํด ์นด์นด์ค ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์์ฒญํจ
- ์ฌ์ฉ์ ์ ๋ณด๋
id
, nickname
, email
, profile_image
๋ฑ
- ์๋ต ๊ฒฐ๊ณผ๋ DTO๋ก ๋งคํํ์ฌ Thymeleaf ๋ฑ ํ
ํ๋ฆฟ์ ์ ๋ฌ ๊ฐ๋ฅ
โ
2. API ์์ฒญ ์ ๋ณด
ํญ๋ชฉ | ๊ฐ |
---|
์์ฒญ URL | https://kapi.kakao.com/v2/user/me |
METHOD | POST ๋๋ GET (POST ๊ถ์ฅ) |
์ธ์ฆ ๋ฐฉ์ | Authorization: Bearer {ACCESS_TOKEN} |
โ
3. ์์ฒญ ํค๋
ํค๋ | ์ค๋ช
| ํ์ |
---|
Authorization | ์ก์ธ์ค ํ ํฐ (Bearer ... ) | โ
|
Content-Type | application/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/>
<div th:text="${profile}"></div>
</body>
</html>
๐ฅ ์ ๋ฆฌ
- ์ก์ธ์ค ํ ํฐ์ ํตํด ์ฌ์ฉ์ ์ ๋ณด ์์ฒญ์ ์ํ
- ์๋ต์ DTO๋ก ๋งคํํ์ฌ Java ๊ฐ์ฒด๋ก ์ฒ๋ฆฌ ๊ฐ๋ฅ
- ๋ก๊ทธ์ธ ํ ์ฌ์ฉ์์ ์ด๋ฉ์ผ, ๋๋ค์, ํ๋กํ ๋ฑ์ ์น์ ์ถ๋ ฅํ ์ ์์
๐ ์ฐธ๊ณ ์๋ฃ