Rest api 코드 발급 후 사용자 토큰 받기 401 에러

개발 환경 : React

요청코드 :

const response = await axios({
    url: KAKAO_URL,
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
    },
    data: JSON.stringify({
      grant_type: "authorization_code",
      client_id: app_key,
      redirect_uri: local_network_url + "/Login#",
      code: kakaoCode
    })
  });

받은 response

Request URL: https://kauth.kakao.com/oauth/token
Request Method: POST
Status Code: 401 Unauthorized

{error: "invalid_client", error_description: "Bad client credentials"}

도저히 어떤 문제인지 파악이 안됩니다. 급하게 출시해야 하는데 여기서 막히니 막막하네요…

혹시 Request 확인 부탁드려도 될까요?

보낸 formdata는 다음과 같습니다.

{"grant_type":"authorization_code","client_id":"********************","redirect_uri":"http://13.124.165.4:3000/Login","code":"********************************************"}

제대로 보낸 것 같은데 왜 문제가 발생하는지 모르겠습니다…
매번 코드도 새로 갱신하는데 혹시 알아봐주실수 있나요?

위 코드를 보면 request시 json으로 보내셨던것 같은데요, form 방식으로 보내셔야 할것 같아요.

curl로 하자면 다음과 같아요.

curl -v -X POST https://kauth.kakao.com/oauth/token \
 -d 'grant_type=authorization_code' \
 -d 'client_id={app_key}' \
 -d 'redirect_uri={redirect_uri}' \
 -d 'code={authorize_code}'

<—

grant_type=authorization_code&client_id=xxx&...
1개의 좋아요