구글 소셜 로그인을 구현하다가 아래와 같은 에러를 만났습니다.
400. That’s an error.
The server cannot process the request because it is malformed.
It should not be retried.
문제 원인, 기존에 사용한 URL:
$oauth_url = "https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount";
이 경로는 구글 내부에서 계정 선택 화면으로 리다이렉트할 때 사용하는 경로입니다.
직접 저 URL을 호출하면
👉 400 malformed request 에러가 발생할 수 있습니다.
해결 방법
OAuth 인증 엔드포인트를 아래처럼 수정합니다.
$oauth_url = "https://accounts.google.com/o/oauth2/v2/auth";
그리고 계정 선택 화면이 필요하다면
prompt=select_account 파라미터를 추가하면 됩니다.
수정된 전체 코드 예시 (PHP)
public function getRequestUrl() {
$oauth_url = "https://accounts.google.com/o/oauth2/v2/auth";
$args = array(
'client_id' => $this->client_id,
'redirect_uri' => $this->redirect_url,
'response_type' => 'code',
'scope' => 'openid email profile',
'prompt' => 'select_account'
);
$url = add_query_arg($args, $oauth_url);
return $url;
}
추가 체크사항
1️⃣ redirect_uri는 콘솔에 정확히 등록되어 있어야 함
Google Cloud Console → OAuth 2.0 클라이언트 → 승인된 리디렉션 URI
- http / https 구분
- www 유무
- 마지막 /
- 쿼리스트링 포함 여부
한 글자라도 다르면 400 에러 발생
scope는 이렇게 쓰는 것이 권장
기존 방식:
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile
권장 방식:
openid email profile
26.02.21 오늘 알았는데 oauthchooseaccount 때문에 에러 나는 경우가 있습니다.
잠수함 패치를 했나 모르겠는데, 아무튼 위 내용처럼 수정을 하면 됩니다.