https://console.cloud.google.com/apis/credentials/consent?authuser=1&project=finhub-430314
Google 클라우드 플랫폼
로그인 Google 클라우드 플랫폼으로 이동
accounts.google.com
기존 카카오 OAuth2.0을 도입한 이후, 앱스토어, 플레이스토어에 승인받기 위해서 구글 로그인과 애플 로그인을 추가로 도입해야하는 상황에 직면했다.
먼저 구글 OAuth 먼저 개발을 시작했다. 콘솔에서 프로젝트를 생성하고, 동의항목을 추가하고, 승인 URI를 설정한 뒤, 코드를 작성했다.
기존 카카오 로그인 프로세스만 있던 코드에서 공통적으로 사용되는 부분은 클래스로 묶어버리고, 최대한 깔끔하게 코드를 작성하려고 노력했다.
그러던 중 에러 메세지를 직면했다. "invalid_grant", "Malformed auth code.
org.springframework.web.client.HttpClientErrorException$BadRequest:
400 Bad Request: "{<EOL> "error": "invalid_grant",<EOL> "error_description": "Malformed auth code."<EOL>}"
https://developers.google.com/identity/protocols/oauth2/web-server?hl=ko
웹 서버 애플리케이션용 OAuth 2.0 사용 | Authorization | Google for Developers
이 페이지는 Cloud Translation API를 통해 번역되었습니다. 의견 보내기 웹 서버 애플리케이션용 OAuth 2.0 사용 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 이
developers.google.com
공식문서에서 에러발생 케이스들을 훑어보니 결국엔 인가코드에 문제가 있다는 점이었다.
4%2F0AcvDMrCHRkVp5EFa6UP1XwZh2j-dT3uEsou4Kx7xkm7DfhWKm0ufpkLKwrXv3tOF03KMLg
이런 형식으로 인가코드를 받아오게 되는데, % 기호가 마음에 걸렸다. 파라미터로 인가코드를 받아와서 백엔드에서 처리해야 할텐데, 저 문자가 잘 인식이 될까? 라는 생각이 들었다.
stackoverflow 를 찾아보니, 아니나다를까 수신한 String code를 디코딩 진행한 이후, google 서버로 전송해보라는 글을 읽게 되었다. URLDeocde의 decode함수를통해서 문제를 해결했다.
private String getGoogleAccessToken(String code, String origin) throws JsonProcessingException {
String decode = URLDecoder.decode(code, StandardCharsets.UTF_8);
HttpHeaders headers = new HttpHeaders();
String redirectUri = getGoogleRedirectUri(origin);
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
Map<String, String> body = new HashMap<>();
body.put("client_id", googleConfig.getClientId());
body.put("client_secret", googleConfig.getClient_secretId());
body.put("code", decode);
body.put("grant_type", googleConfig.getGrant_type());
body.put("redirect_uri", redirectUri);
return oAuth2Util.getAccessToken(googleConfig.getAccessTokenRequestUrl(), headers, body);
}
진행 중인 프로젝트 깃허브 URL이다.
https://github.com/fotcamp/Finhub-Backend
GitHub - fotcamp/Finhub-Backend: Finhub API Server
Finhub API Server. Contribute to fotcamp/Finhub-Backend development by creating an account on GitHub.
github.com
'Backend' 카테고리의 다른 글
| [Springboot] OAuth2.0 도입 - RestTemplate, FeignClient 비교 (1) | 2024.07.28 |
|---|---|
| [Springboot] Apple 로그인 OAuth2.0 도입 (0) | 2024.07.26 |
| [Springboot] DTO-Entity 변환 위치는 어디가 적합할까 (2) | 2024.07.03 |
| [Springboot] 난수 생성엔 Random보다 SecureRandom을 사용하자. (0) | 2024.07.03 |
| [Springboot] Logback (1) | 2024.06.30 |