988974
리액트+노드JS
문의 시, 사용하시는 개발환경과 디벨로퍼스 앱ID를 알려주세요.
import React, { useEffect } from ‘react’
import { useDispatch } from ‘react-redux’
import axios from ‘axios’;
// import { actionCreators as userActions } from ‘…/redux/modules/user’;
const kakao_REST_API_KEY = ‘f5810145dffc679dc95abf173323705a’;
const kakao_REDIRECT_URI = ‘http://localhost:3000/login/oauth/callback/kakao’;
const kakaoLink = https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=${kakao_REST_API_KEY}&redirect_uri=${kakao_REDIRECT_URI}
;
const OAuthRedirectHandler = (props) => {
const dispatch = useDispatch();
// React.useEffect(async () => {
// await dispatch(userActions.kakaoLogin(authCode));
// }, [])
console.log(('hi'))
const getToken = async (code) => {
const result = await axios.post(
`https://kauth.kakao.com/oauth/token`,
{
grant_type: 'authorization_code',
client_id: kakao_REST_API_KEY,
redirect_uri: kakao_REDIRECT_URI,
code,
},
);
return result.data.access_token;
};
const getUserInfo = async (access_token) => {
const result = await axios.get(
`https://kapi.kakao.com/v2/user/me`,
{
headers: {
Authorization: `Bearer ${access_token}`,
},
},
);
return result.data;
};
useEffect(() => {
const url = new URL(window.location.href);
const authorizationCode = url.searchParams.get('code');
if (authorizationCode) {
// console.log(authorizationCode)
const fetchUser = async () => {
try {
console.log('ㅇ'+authorizationCode)
const token = await getToken(authorizationCode); <<<이부분이 실행이 안되고 401에러가뜹니다...
console.log('ㅇ22'+authorizationCode)
const userInfo = await getUserInfo(token);
console.log(userInfo);
} catch (err) {
console.error('에러??'+err);
}
};
fetchUser();
}
}, []);
return (
<div>로딩중</div>
)
}
export default OAuthRedirectHandler