Skip to content

Commit

Permalink
✨ 카카오 유저 정보 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
daangn-william authored and rilac1 committed Apr 2, 2023
1 parent 6f747ba commit 421262a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package yapp.allround3.auth.oauth.client;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import yapp.allround3.common.exception.CustomException;

@Component("KAKAO")
@Slf4j
public class KakaoUserInfoClient implements OauthUserInfoClient {

private final ObjectMapper objectMapper = new ObjectMapper();
public OauthUserInfo getUserInfo(String accessToken) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer " + accessToken);
headers.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

HttpEntity<MultiValueMap<String, String>> kakaoUserInfoRequest = new HttpEntity<>(headers);
RestTemplate rt = new RestTemplate();
ResponseEntity<String> response = rt.exchange(
"https://kapi.kakao.com/v2/user/me",
HttpMethod.POST,
kakaoUserInfoRequest,
String.class
);

String body = response.getBody();
try {
JsonNode jsonNode = objectMapper.readTree(body);
String id = jsonNode.get("id").asText();
String nickname = jsonNode.get("properties").get("nickname").asText();
String email = jsonNode.get("kakao_account").get("email").asText();
String imageUrl = jsonNode.get("properties").get("profile_image").asText();

return OauthUserInfo.builder()
.oauthId(id)
.nickname(nickname)
.email(email)
.imageUrl(imageUrl)
.build();
} catch (Exception e) {
throw new CustomException("Oauth 회원 정보 조회 에러");
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/yapp/allround3/auth/oauth/client/OauthUserInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package yapp.allround3.auth.oauth.client;

import lombok.Builder;
import lombok.Getter;
import yapp.allround3.auth.oauth.Provider;
import yapp.allround3.member.domain.Member;

@Getter
@Builder
public class OauthUserInfo {
private final String nickname;
private final String email;
private final String imageUrl;
private final Provider provider;
private final String oauthId;

public Member toMember() {
return Member.builder()
.name(nickname)
.email(email)
.imageUrl(imageUrl)
.provider(provider)
.oauthId(oauthId)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package yapp.allround3.auth.oauth.client;

public interface OauthUserInfoClient {
OauthUserInfo getUserInfo(String accessToken);
}

0 comments on commit 421262a

Please sign in to comment.