카카오 로그인은 카카오계정의 사용자 자격정보로 인가 코드를 받아오고, 인가 코드로 액세스 토큰과 리프레시 토큰을 얻는 과정으로 구성돼 있습니다. 액세스 토큰은 사용자를 인증하고 카카오 API 호출 권한을 부여합니다. 리프레시 토큰(Refresh Token)은 사용자가 매번 카카오계정 정보를 입력하거나 카카오톡으로 로그인하지 않고도 액세스 토큰을 발급받을 수 있게 합니다.
fn_snsLogin('kakao');
실행 하도록 하기<div class="social-container">
<div class="col">
<img src="${path }/resources/images/login/kakao.png"
alt="카카오로그인" class="sns-icon" onclick="fn_snsLogin('kakao');"/>
</div>
</div>
<script>
function fn_snsLogin(sns){
let sendUrl="";
console.log(sns);
if(sns=="naver"){
sendUrl="${naver_url }";
}else if(sns=="kakao"){
sendUrl="https://kauth.kakao.com/oauth/authorize?client_id=RESTAPI키&redirect_uri=등록한 리다이렉트URI&response_type=code";
}else if(sns=="google"){
sendUrl= googleUrl="${google_url}";
}
location.replace(sendUrl);
}
</script>
인가 코드를 받기 위한 Request
- URIGET /oauth/authorize? client_id={REST_API_KEY} &redirect_uri={REDIRECT_URI}&response_type=code HTTP/1.1 Host: kauth.kakao.com
- 실제 요청 주소
https://kauth.kakao.com/oauth/authorize?client_id=REST API키&redirect_uri=https://rclass.iptime.org/20PM_BOM_final/auth/kakao/callback&response_type=code
- 요청시 파라미터
response 응답 : 로그인 성공시
HTTP/1.1 302 Found Content-Length: 0 Location: {REDIRECT_URI}?code={AUTHORIZE_CODE}
해당 API는 인가 코드로 액세스 토큰과 리프레시 토큰을 발급받는 API.
필수 파라미터 값들을 담아 POST로 요청.
요청 성공 시, 응답은 JSON
객체로 Redirect URI
에 전달되며 두 가지 종류의 토큰 값과 타입, 초 단위로 된 만료 시간을 포함함.
//SnsController.java
@RequestMapping("/auth/kakao/callback")
public String kakaoLogin(Model model,
@RequestParam(value = "code", required = false) String code,
HttpServletRequest request) throws Exception{
//상제로직 작성란...
}
//SnsController.java
//인가코드 매개변수로 전달해서 access_token 받아오기
String access_Token=kakaoService.getAccessToken(code);
토큰 받아오는 방법
POST /oauth/token HTTP/1.1
Host: kauth.kakao.com
Content-type: application/x-www-form-urlencoded;charset=utf-8
Request
curl -v -X POST "https://kauth.kakao.com/oauth/token" \ -d "grant_type=authorization_code" \ -d "client_id={REST_API_KEY}" \ -d "redirect_uri={REDIRECT_URI}" \ -d "code={AUTHORIZATION_CODE}"
Response
HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 { "token_type":"bearer", "access_token":"{ACCESS_TOKEN}", "expires_in":43199, "refresh_token":"{REFRESH_TOKEN}", "refresh_token_expires_in":25184000, "scope":"account_email profile" }
GET/POST /v2/user/me HTTP/1.1
Host: kapi.kakao.com
Authorization: Bearer {ACCESS_TOKEN}
Content-type: application/x-www-form-urlencoded;charset=utf-8
//KakaoController.java
//access_token으로 사용자 정보 가져오기
HashMap<String,Object> userInfo=kakaoService.getUserInfo(access_Token);
응답예제
json{4 items
"id":float9876543211234
"properties":{3 items
"nickname":string"홍길동"
"thumbnail_image":string"http://xxx.kakao.co.kr/.../aaa.jpg"
"profile_image":string"http://xxx.kakao.co.kr/.../bbb.jpg"
}
"kakao_account":{9 items
"profile_needs_agreement":boolfalse
"profile":{1 item
"nickname":string"jaden.h"
}
"has_birthday":boolfalse
"birthday_needs_agreement":boolfalse
"has_ci":boolfalse
"ci_needs_agreement":boolfalse
"legal_name_needs_agreement":boolfalse
"legal_birth_date_needs_agreement":boolfalse
"legal_gender_needs_agreement":boolfalse
}
"connected_at":string"2020-03-04T05:45:55Z"
}
실제 적용코드
//KakaoService.java
public HashMap<String, Object> getUserInfo (String access_Token) {
HashMap<String, Object> userInfo = new HashMap<>();
String reqURL = "https://kapi.kakao.com/v2/user/me";
try {
URL url = new URL(reqURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//요청 메소드
conn.setRequestMethod("POST");
//요청에 필요한 Header에 포함될 내용
conn.setRequestProperty("Authorization", "Bearer " + access_Token);
//응답완료코드
int responseCode = conn.getResponseCode();
System.out.println("responseCode : " + responseCode);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
String result = "";
while ((line = br.readLine()) != null) {
result += line;
}
System.out.println("response body : " + result);
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(result);
String id = element.getAsJsonObject().get("id").getAsString();
JsonObject properties = element.getAsJsonObject().get("properties").getAsJsonObject();
JsonObject kakao_account = element.getAsJsonObject().get("kakao_account").getAsJsonObject();
String email=null;
if(kakao_account.getAsJsonObject().get("email")!=null) {
email = kakao_account.getAsJsonObject().get("email").getAsString();
}
String nickname = properties.getAsJsonObject().get("nickname").getAsString();
String profile_image = properties.getAsJsonObject().get("profile_image").getAsString();
userInfo.put("id", id);
userInfo.put("email", email);
userInfo.put("nickname", nickname);
userInfo.put("profile_image", profile_image);
} catch (IOException e) {
e.printStackTrace();
}
return userInfo;
}
출처 및 참고 사이트