|
3 | 3 | import com.semosan.api.common.exception.GeneralException; |
4 | 4 | import com.semosan.api.domain.oauth.properties.KakaoProperties; |
5 | 5 | import com.semosan.api.common.status.ErrorStatus; |
6 | | -import com.semosan.api.domain.oauth.dto.KakaoTokenResponse; |
7 | 6 | import com.semosan.api.domain.oauth.dto.KakaoUserInfoResponse; |
8 | 7 | import lombok.RequiredArgsConstructor; |
9 | 8 | import lombok.extern.slf4j.Slf4j; |
10 | 9 | import org.springframework.http.HttpStatusCode; |
11 | 10 | import org.springframework.stereotype.Component; |
12 | | -import org.springframework.util.LinkedMultiValueMap; |
13 | | -import org.springframework.util.MultiValueMap; |
14 | | -import org.springframework.util.StringUtils; |
15 | 11 | import org.springframework.web.reactive.function.BodyInserters; |
16 | 12 | import org.springframework.web.reactive.function.client.WebClient; |
17 | 13 | import org.springframework.web.reactive.function.client.WebClientResponseException; |
|
22 | 18 | @RequiredArgsConstructor |
23 | 19 | public class OAuthKakaoClient { |
24 | 20 |
|
25 | | - private final WebClient kakaoAuthWebClient; |
26 | 21 | private final WebClient kakaoApiWebClient; |
27 | 22 | private final KakaoProperties kakaoProperties; |
28 | 23 |
|
29 | | - // 인가 코드 -> 카카오 액세스 토큰 |
30 | | - public KakaoTokenResponse getKakaoToken(String code) { |
31 | | - MultiValueMap<String, String> form = new LinkedMultiValueMap<>(); |
32 | | - form.add("grant_type", "authorization_code"); |
33 | | - form.add("client_id", kakaoProperties.clientId()); |
34 | | - form.add("redirect_uri", kakaoProperties.redirectUri()); |
35 | | - form.add("code", code); |
36 | | - if (StringUtils.hasText(kakaoProperties.clientSecret())) { |
37 | | - form.add("client_secret", kakaoProperties.clientSecret()); |
38 | | - } |
39 | | - |
40 | | - try { |
41 | | - KakaoTokenResponse response = kakaoAuthWebClient.post() |
42 | | - .uri("/oauth/token") |
43 | | - .body(BodyInserters.fromFormData(form)) |
44 | | - .retrieve() |
45 | | - .onStatus( |
46 | | - HttpStatusCode::isError, |
47 | | - r -> r.bodyToMono(String.class) |
48 | | - .flatMap(body -> handleError(r.statusCode(), body, "토큰 발급")) |
49 | | - ) |
50 | | - .bodyToMono(KakaoTokenResponse.class) |
51 | | - .block(); |
52 | | - |
53 | | - if (response == null || !StringUtils.hasText(response.accessToken())) { |
54 | | - throw new GeneralException(ErrorStatus.KAKAO_TOKEN_REQUEST_FAILED); |
55 | | - } |
56 | | - return response; |
57 | | - } catch (WebClientResponseException e) { |
58 | | - log.error("[*] 카카오 토큰 발급 실패 status={}, body={}", e.getStatusCode(), e.getResponseBodyAsString()); |
59 | | - throw new GeneralException(ErrorStatus.KAKAO_TOKEN_REQUEST_FAILED); |
60 | | - } |
61 | | - } |
62 | | - |
63 | 24 | // 카카오 액세스 토큰 -> 사용자 정보 |
64 | 25 | public KakaoUserInfoResponse getKakaoUserInfo(String kakaoAccessToken) { |
65 | 26 | try { |
|
0 commit comments