access_Token는 문제없이 받아올 수 있습니다만, userInfo 함수에서 java.io.IOException: Server returned HTTP response code: 400 for URL: https://kapi.kakao.com/v2/user/me 오류가 납니다

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 가 발생하는데 혹시 원일을 알 수있을까요?
  1. “Bearer”+access_Token);
    => Bearer와 access_Token 변수 사이에 공백 한 칸 있어야합니다.
    => "Bearer "+access_Token); 이렇게.
    =>

  2. conn.getResponseMessage() 로 에러코드 보다 자세한 메세지를 확인해보세요.

resonponse body 를 확인해보았습니만, response message : Unauthorized 와 같은 401 error 메세지가 연출됩니다.

image

로그를 보니… 토큰을 받은 후, 프로필 조회 시 401이네요.

이것 저것 테스트 해보니

토큰을 성공적으로 조회 하고 프로필 조회 에서 401 (Unauthorized) 떨어지는 것은

프로필 조회 요청 시, access_token을 사용하지 않고 refresh_token을 사용했을 때 밖에 없네요.

프로필 조회 시, 세팅하는 헤더 값도 로그에 찍어서 한번 확인해보시죠~~