사용자 정보들고오기 400에러

@RequestMapping("/app")
public String app(HttpServletRequest request , HttpServletResponse response) {
	final String AUTH_HOST = "https://kauth.kakao.com";
    final String tokenRequestUrl = AUTH_HOST + "/oauth/token";

    String CLIENT_ID = "8d5b47c9fabfc0eae6ac2477fe0f1453"; // 해당 앱의 REST API KEY 정보. 개발자 웹사이트의 대쉬보드에서 확인 가능
    String REDIRECT_URI = "http://spiritlook.cafe24.com/app"; // 해당 앱의 설정된 uri. 개발자 웹사이트의 대쉬보드에서 확인 및 설정 가능
    String code = request.getQueryString(); // 로그인 과정중 얻은 authorization code 값
    String getPersnalInfoUrl = "	https://kapi.kakao.com/v1/user/me"; // 로그인후 사용자 정보 받아 오기위한 url 
    String access_token = "access_token";
    String token_type = "token_type";
	log.info(code);
    	
    HttpsURLConnection conn = null;
    OutputStreamWriter writer = null;
    BufferedReader reader = null;
    InputStreamReader isr= null;
     URL url = null;
    try {
      final String params = String.format("grant_type=authorization_code&client_id=%s&redirect_uri=%s&",
                        CLIENT_ID, REDIRECT_URI) + code;

      url = new URL(tokenRequestUrl);

      conn = (HttpsURLConnection) url.openConnection();
      conn.setRequestMethod("POST");
      conn.setDoOutput(true);

      writer = new OutputStreamWriter(conn.getOutputStream());
      writer.write(params);
      writer.flush();

      final int responseCode = conn.getResponseCode();
      System.out.println("Sending 'POST' request to URL : " + tokenRequestUrl);
      System.out.println("Post parameters : " + params);
      System.out.println("Response Code : " + responseCode);

      isr = new InputStreamReader(conn.getInputStream());
      reader = new BufferedReader(isr);
      final StringBuffer buffer = new StringBuffer();
      String line;
      while ((line = reader.readLine()) != null) {
        buffer.append(line);
      }
      String responseParam = buffer.toString();
      System.out.println(responseParam);
	  access_token = responseParam.substring(responseParam.indexOf("access_token") + access_token.length() + 3 , responseParam.indexOf(token_type) - 3);
	  log.info("access_token:" + access_token);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
        // clear resources
        if (writer != null) {
          try {
              writer.close();
           } catch(Exception ignore) {
           }
        }
        if (reader != null) {
          try {
              reader.close();
          } catch(Exception ignore) {
          }
        }
        if (isr != null) {
            try {
                isr.close();
            } catch(Exception ignore) {
            }
         }
    }
    
    conn.disconnect();
    
    
   

    try {
		url = new URL(getPersnalInfoUrl);
	    conn = (HttpsURLConnection) url.openConnection();
	    conn.setRequestProperty("Authorization", "Bearer "  +access_token.trim());
	    conn.setRequestProperty("Content-Type", "x-www-form-urlencoded");
	    conn.setRequestMethod("GET");
	    conn.setDoOutput(true);
	    
	      writer = new OutputStreamWriter(conn.getOutputStream());
	      writer.flush();
	      
	      log.info("header 전송");

	      int responseCode = conn.getResponseCode();
	      System.out.println("Sending 'POST' request to URL : " + getPersnalInfoUrl);
	      System.out.println("Response Code : " + responseCode);
	      

	      isr = new InputStreamReader(conn.getInputStream());
	      reader = new BufferedReader(isr);
	      final StringBuffer buffer = new StringBuffer();
	      String line;
	      while ((line = reader.readLine()) != null) {
	        buffer.append(line);
	      }
	      String responseParam = buffer.toString();
	      System.out.println(responseParam);

	    

	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

    
	return "app";
}

@tjdrhs333

아래와 같이 수정해 보세요.

  final String params = String.format("grant_type=authorization_code&client_id=%s&redirect_uri=%s&",
                    CLIENT_ID, REDIRECT_URI) + "&code=" + code;

params 가 사용자 정보를 들고올땐 아예 필요없지않나요? 그래서 저 파람스는 access token을 받을때 이용하여서 잘 받아 오고있습니다.

@tjdrhs333
params는 사용자 정보 가져올 땐 필요 없습니다.

님이 수정하라 하신건 ㅠㅠ access_token인데 access_token은 잘들고 와집니다 ㅠㅠ 그후
사용자 정보가 안들고와집니다. ㅠㅠ

    String access_token = "access_token";
    String token_type = "token_type";
	  access_token = responseParam.substring(responseParam.indexOf("access_token") + access_token.length() + 3 , responseParam.indexOf(token_type) - 3);
	    conn.setRequestProperty("Bearer ",access_token.trim());
	    //conn.setRequestProperty("KakaoAK","8d5b47c9fabfc0eae6ac2477fe0f1453");
	    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	    conn.setRequestMethod("POST");

이렇게 보내도 401 에러가 납니다 401에러 unAuthorization이라는데 …

@tjdrhs333

onn.setRequestProperty("Bearer ",access_token.trim());

이 부분을 아래와 같이 수정하세요. Authorization 헤더 이름에 값은 ‘Bearer [access_token]’ 이어야 합니다.

onn.setRequestProperty("Authorization", "Bearer " + access_token.trim());