문의 시, 사용하시는 SDK 버전 정보와 디벨로퍼스 앱ID를 알려주세요.
<script src="https://t1.kakaocdn.net/kakao_js_sdk/2.7.4/kakao.min.js"
integrity="sha384-DKYJZ8NLiK8MN4/C5P2dtSmLQ4KwPaoqAfyA/DfmEc1VDxu4yyC7wy6K1Hs90nka" crossorigin="anonymous">
</script>
ID 1204996
로그인 할 때는,
document.addEventListener("DOMContentLoaded", function() {
if (!Kakao.isInitialized()) {
Kakao.init('${kakaoJspKey}');
}
Kakao.Auth.authorize({
redirectUri: '${redirectUri}',
});
});
javascript 코드로 진행하다가
callback 받아서는 code로 restAPI key로 access token 값을 가져옵니다.
$(document).ready(function(){{
$.ajax({
type: "post", // GET 또는 POST
url: "https://kauth.kakao.com/oauth/token", // 데이터를 요청할 API, url
beforeSend : function(xhr){
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
},
data: {
grant_type: 'authorization_code',
client_id: '${kakaoRestApiKey}',
redirect_uri: '${redirectUri}',
code: '${code}'
},
success: function (data) {
if (data.access_token) {
localStorage.setItem('kakao_access_token', data.access_token);
getUserInfo(data.access_token);
}
}, error: function(error){
console.log("error : ", error);
}
});
}});
가져온 access token으로 userInfo를 조회해서 로그인 합니다.
function getUserInfo(accessToken) {
$.ajax({
type: "get", // GET 또는 POST
url: "https://kapi.kakao.com/v2/user/me", // 데이터를 요청할 API, url
beforeSend : function(xhr){
xhr.setRequestHeader("Authorization",`Bearer `+accessToken);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
},
success: function (data) {
const userId = data.id;
const nickname = data.kakao_account.profile.nickname;
// 여기서 서버(Java)로 전송하여 저장 가능
if (window.opener) {
window.opener.postMessage({ userId: userId, nickname: nickname }, "*");
// 팝업 닫기
window.close();
}
}, error: function(error){
console.log("error : ", error);
}
});
}
이렇게 로그인을 구현해놨는데
javascript와 restApi의 키를 혼용하고 있기 때문일까요?
아니면 sdk 버전이 v1이 아니라 v2 이기 때문일까요?
분명 로그인이 되어있는 상태인데
Kakao.Auth.getAccessToken()을 확인하면 null이네요.
function exitKakao(){
if (!Kakao.isInitialized()) {
Kakao.init('${kakaoJspKey}');
}
console.log('접근 토큰 : ', Kakao.Auth.getAccessToken())
Kakao.Auth.logout()
.then(function(response) {
console.log(Kakao.Auth.getAccessToken()); // null
})
.catch(function(error) {
console.log('Not logged in.');
});
}
https://kapi.kakao.com/v1/user/logout 401 (Unauthorized)
에러가 납니다.
혹시나 해서
if (!Kakao.isInitialized()) {
Kakao.init('${kakaoJspKey}');
}
에서 jsp key 값 대신 restApi key값으로 init해도 똑같습니다.
어떻게 해야 로그아웃을 할 수 있을까요?