Yana94Ko / cuokkaMap

B-side 303TEN프로젝트 "커카맵"
1 stars 2 forks source link

[BE, KAKAO_LOGIN] 로그인 후 사용자 정보 담기 #17

Closed github-actions[bot] closed 1 year ago

github-actions[bot] commented 1 year ago

관련해 VO 생성이 우선되어야 하기에 우선 현재까지 작업을 먼저 올린 후

모델링에 맟추어 VO를 작성할 예정

https://api.github.com/Yana94Ko/cuokkaMap/blob/b84d8457a41f064325d0d46609788dce3ac2c8c6/src/main/java/com/bside/cuokkamap/service/KakaoAPI.java#L93


package com.bside.cuokkamap.service;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

@Service
public class KakaoAPI {
    @Value("${KAKAO_LOGIN_REST_API_KEY}")
    private String KAKAO_LOGIN_REST_API_KEY;

    @Value("${KAKAO_LOGIN_REDIRECT_URI}")
    private String KAKAO_LOGIN_REDIRECT_URI;

    public JSONObject getToken (String authorizeCode) {
        StringBuffer sb = new StringBuffer();
        sb.append("grant_type=authorization_code");
        sb.append("&client_id"+KAKAO_LOGIN_REST_API_KEY);
        sb.append("&redirect_uri"+KAKAO_LOGIN_REDIRECT_URI);
        sb.append("&code="+authorizeCode);

        return getTokenJson(sb);
    }

    public JSONObject getTokenJson(StringBuffer sb) {
        String tokenUrl = "https://kauth.kakao.com/oauth/token";
        String method = "POST";

        JSONObject tokenJson = null;
        try {
            URL url = new URL(tokenUrl);

            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setDoOutput(true);
            con.setRequestMethod(method);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
            bw.write(sb.toString());
            bw.flush();

            StringBuffer response = getResponse(con);
            tokenJson = new JSONObject(response.toString());
        } catch (Exception e) {
           e.printStackTrace();
        }

        return tokenJson;
    }

    public StringBuffer getResponse (HttpURLConnection con) throws IOException {
        int responseCode = con.getResponseCode();
        BufferedReader br = null;

        if ( responseCode == 200 ) {
            br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        } else {
            br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        }

        StringBuffer response = new StringBuffer();
        while(br.ready()) {
            response.append(br.readLine());
        }

        return response;
    }

    //oauth 통해서 받은 accessToken 으로 사용자 정보 받아오기
    public HashMap<String, String> getUserInfo (String accessToken) {
        String infoUrl = "https://kapi.kakao.com/v2/user/me";
        String method = "GET";

        HashMap <String, String> userInfo = new HashMap<>();
        try {
            URL url = new URL(infoUrl);

            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setUseCaches(false);
            con.setRequestMethod(method);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            con.setRequestProperty("Authorization", "Bearer"+accessToken);

            StringBuffer response = getResponse(con);
            JSONObject userJson = new JSONObject(response.toString());
            JSONObject profileJson = (JSONObject) userJson.get("properties");

            // TODO(BE, KAKAO_LOGIN) : 로그인 후 사용자 정보 담기
            // 관련해 VO 생성이 우선되어야 하기에 우선 현재까지 작업을 먼저 올린 후
            // 모델링에 맟추어 VO를 작성할 예정

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
github-actions[bot] commented 1 year ago

Closed in 280a1a84248e0e63dc8d170cee3cde17972f965b