Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: [no body]] with root cause ...
RestTemplate Class로 카카오 로그인을 구현하는 과정에서 발생하였다.
카카오 인증 서버에 토큰은 잘 받아 왔는데, 그 이후에 회원 정보를 가져오는 과정에서 Body가 존재하지 않는다는 에러를 만났다.
아래의 코드가 에러를 만났을 때의 코드다.
public KakaoProfile findProfile(String token) {
RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + token);
KakaoProfile kakaoProfile = template.postForObject(userInfoUri, headers, KakaoProfile.class);
return kakaoProfile;
}
해당 에러가 Body가 없다는 에러이기 때문에,
아래처럼 HttpEntity로 Body에 null 값을 넣어주었다.
그랬더니 잘 해결이 되었다.
public KakaoProfile findProfile(String token) {
RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + token);
HttpEntity<MultiValueMap<String, String> > requestEntity = new HttpEntity<>(null, headers);
KakaoProfile kakaoProfile = template.postForObject(userInfoUri, requestEntity, KakaoProfile.class);
return kakaoProfile;
}
'에러 기록' 카테고리의 다른 글
[에러 기록] does not have a commit checked out (1) | 2023.12.05 |
---|---|
[에러 기록] HttpMessageNotReadableException: JSON parse error (0) | 2023.01.09 |
댓글