REST API 로 로그인시, 401 error , Unauthorized for Url https://kauth.kakao.com/oauth/token

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}’

가이드의 위부분 보고 하였습니다. 아이오닉 하이브리드 웹이구요.

왜그럴까요…? 리퀘스트 토큰은 잘 가져오는것 확인하였습니다.

코드로 요청은 다음과같이 하였습니다.

var ref = window.open('https://kauth.kakao.com/oauth/authorize?client_id='+this.client_id+'&redirect_uri='+this.redirect_uri+'&response_type=code', '_blank', 'location=no');
      ref.addEventListener('loadstart', (event: InAppBrowserEvent) =>{ 
          if((event.url).startsWith("http://localhost/")) {
              var requestToken = (event.url).split("code=")[1];
             //토큰을 잘 가져오고있습니다. 
              this.http.post("https://kauth.kakao.com/oauth/token",{params:"grant_type=authorization_code"+"&client_id=" + this.client_id + "&redirect_uri="+this.redirect_uri+"&code="+requestToken},options).toPromise().then((res)=>{
                    //이부분으로 오지 않고 401에러가 발생하네요 
                console.log(res.json())
                    alert("success")
              }).catch((error)=>{
                alert(error);
              })
              ref.close();
          }
      });

아. 추가적으로 간단한 질문드립니다.

위와같은 방법으로 로그인이 아닌…카카오톡 로그인버튼을 누를시, 카카오톡 앱이 켜지고 거기에서 승인 받고 하는 경우를 많은 앱에서 본적이있는데요…그건 어떻게 하는걸까요?

1개의 좋아요

@kotran 간편로그인 부분은 왠지 아래 3중에 하나일것 같아요.

https://devtalk.kakao.com/t/topic/20684/14

https://devtalk.kakao.com/t/topic/19931/25

https://devtalk.kakao.com/t/javascript-id-password/17260/5

그런데 하이브리드 앱에서는 안될 가능성이 많네요. 보통 user agent를 통해서 기본 브라우저나 특수 브라우저에서만 지원되게 하였거든요. 커스텀인앱브라우저나 다른 브라우저의 경우 오류가 많아서요 ㅠㅜ

해결책은 없나본가요…

실기기에서도 테스트하니 똑같은 결과 나오는군요
URL 에 권한이 없다니…음…ㅜㅠ

네이버는 플러그인이 있던데 그걸 써야겠네요 ㅜㅠ

답변감사합니다.

@kotran 아 제가 답변 드린것은 카카오톡앱을 통한 간편 로그인 부분이고, 현재 401 에러 난다는것은 답을 안드렸어요.
혹시 401날때 response body는 무엇으로 응답 받으셨는지 알 수 있을까요?
추가적으로 401을 재현하시고, 요청하신 authorization code도 알려주시면 원인을 좀 더 찾아볼수 있을것 같아요.

1개의 좋아요

전체 펑션은 아래와같아요.
authorization code라면 request token을 말씀하신건가요?
가이드를 봐도 request token을 get 방식으로 받고 그것을 가지고 post방식으로 access token을 받아야 한다고 나와있어서요.

request token은 요청할때마다 값이 바뀌는데…그거라도 알려드려야할까요?
친절한 답변 감사합니다.

kakaoLogin(){
      let headers = new Headers({ 'Content-Type': 'application/x-www-urlencoded' });
           let options = new RequestOptions({ headers: headers });
      var ref = window.open('https://kauth.kakao.com/oauth/authorize?client_id='+this.client_id+'&redirect_uri='+this.redirect_uri+'&response_type=code', '_blank', 'location=no');
      ref.addEventListener('loadstart', (event: InAppBrowserEvent) =>{ 
          if((event.url).startsWith("http://localhost/")) {
              var requestToken = (event.url).split("code=")[1];
              this.http.post("https://kauth.kakao.com/oauth/token",{params:"grant_type=authorization_code"+"&client_id=" + this.client_id + "&redirect_uri="+this.redirect_uri+"&code="+requestToken},options).toPromise().then((res)=>{
                console.log(res.json())
                alert(res);
              }).catch((error)=>{
                alert(error);
              })
              ref.close();
          }
      });
    }

넵 맞아요. 매번 바뀌겟지만 request token 발급받은 후 access token 요청을 하신 후 실패 되엇을때의 request token를 한번 주세요.

아래와같습니다. 감사합니다

@kotran 아쉽게도 해당 code가 잘 발급됨을 확인하였으나, 신기하게도 그 이후 /oauth/token 요청이 들어오지를 않았네요. 얼핏 봐도 저도 어떤 이슈인지 잘 모르겠어요. 코드로도 큰 문제는 없는것 같고…

  1. 해당 앱, 해당 계정으로 성공(토큰 발급까지)을 하고 잇는것도 있는데 위 이슈는 특수한 케이스에서만 발생하는지요?
  2. 위 코드에서 /oauth/token까지 하지 말고, code만 받고 console등에서 아래와 같이 한번 수동으로 token을 발급받아 보시겠어요?
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}'

이렇게 발급받은 code를 수동으로 /oauth/token 이 성공한다면, 코드나 환경 문제일것 같아요. ㅠㅜ (authorize_code는 일회용이고 만료 기간이 짧음을 고려하셔야 함).

별 도움을 못드려 죄송합니다.

1개의 좋아요