public static HashMap<String, Object> getUserInfo (String access_Token) throws Exception{
//1. 요청하는 클라이언트마다 가진 정보가 다를 수 있기에 HashMap 타입으로 선언!
HashMap<String, Object> userInfo=new HashMap<>();
//2. 하기의 url로 request하여, userInfo의 정보를 가져올 것이기에
// URL을 객체를 통해 하기의 url을 담는다.
String requestURL="https://kapi.kakao.com/v2/user/me";
URL url=new URL(requestURL);
//3. connection 객체 선언
//Java 에서 HTTP로 데이터를 송, 수신하기 위해서는
//HttpURLConnection 클래스를 이용한다.
HttpURLConnection con=(HttpURLConnection) url.openConnection();
// 4. POST 요청을 위해 기본값이 false인 setDoOutput을 true로
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
//4. host server에 request에 필요한 KEY값을 셋팅한다.
con.setRequestProperty("Authorization", "Bearer"+access_Token);
// reqeust : access Token, 모든 정보를 받기위한 용도
con.setRequestProperty("charset", "utf-8");
//2e74016a55faeab59754ac119822f522
// 5.카카오 서버로부터 userInfo에 대한 response가 true일 시 200 아니면 400
int responseCode=con.getResponseCode();
System.out.println("responseCode : " + responseCode);
System.out.println("요청확인");
//6. 이곳이 error 지점
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
String result = null;
while ((line = br.readLine()) != null) {
result += line;
}
System.out.println("response body : " + result);
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(result);
JsonObject kakaoUid=element.getAsJsonObject().get("id").getAsJsonObject();
JsonObject properties = element.getAsJsonObject().get("properties").getAsJsonObject();
JsonObject kakao_account = element.getAsJsonObject().get("kakao_account").getAsJsonObject();
String id=kakaoUid.getAsJsonObject().getAsString();
String nickname = properties.getAsJsonObject().get("nickname").getAsString();
String email = kakao_account.getAsJsonObject().get("email").getAsString();
userInfo.put("kakaoUid", id);
userInfo.put("nickname", nickname);
userInfo.put("email", email);
//URLConnection에 대한 doOutput 필드값을 지정된 값으로 설정한다.
//URL 연결은 입출력에 사용될 수 있다.
//URL 연결을 출력용으로 사용하려는 경우 DoOutput 플래그를 true로 설정하고, 그렇지 않은 경우는 false로 설정해야 한다. 기본값은 false이다.
return userInfo;
}
int responseCode=con.getResponseCode(); 이부분에서 error 가 발생하는데 혹시 원일을 알 수있을까요?