구글 소셜로그인을 구현하던 도중 code를 받아와서 넣어주니
"error_description": "malformed auth code."
이런 에러가 떴다. 다른 블로그도 찾아보고 공식 문서도 뒤져보고 그 원인을 알수 있었는데
코드를 decode를 하여 넣어줘야 코드로 인식을 하여 access_code를 받아올수 있다.
// 코드를 디코드를 하기 위해 추가
String decode = URLDecoder.decode(code, StandardCharsets.UTF_8);
// 원래 있던 코드들
RestTemplate rt = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-type", "application/x-www-form-urlencoded");
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "authorization_code");
params.add("client_id", googleClientId);
params.add("client_secret", googleClient_Secret);
params.add("redirect_uri", googleRedirect_uri);
params.add("code", decode);
HttpEntity<MultiValueMap<String, String>> googleTokenRequest = new HttpEntity<>(params, httpHeaders);
ResponseEntity<String> response = rt.exchange(
"https://oauth2.googleapis.com/token",
HttpMethod.POST,
googleTokenRequest,
String.class
);
위에 코드를 추가한후 access_code를 받아올수 있었다.
