안녕하세요. 서블릿 카카오페이 결제 준비 호출 시 401 에러로 어려움을 겪고있습니다.
아래 소스를 통해서 한번 검토해주시면 감사하겠습니다.
앱키는 제 해당 계성 admin key 이므로 지우겠습니다.
setRequestProperty로 Authorization 를 담아서 넘겨야하는데 실제로
getRequestProperties 로 확인해보면 Authorization은 담기지 않아서 그런 것같습니다.
도움 부탁드립니다.
감사합니다.
@WebServlet("/kakaoPay")
public class kakaoPay extends HttpServlet{
private static final String HOST = "https://kapi.kakao.com/v1/payment/ready";
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
String appKey = "{abcdefgcdf}";
try {
URL obj = new URL(HOST);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "KakaoAK "+appKey);
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
con.setDoInput(true);
con.setDoOutput(true);
// Send post request
JSONObject test = new JSONObject();
System.out.println("RequestProperty -> " + con.getRequestProperty("Authorization"));
System.out.println("method -> " + con.getRequestMethod());
System.out.println("getRequestProperties -> " + con.getRequestProperties());
System.out.println("getResponseCode -> " + con.getResponseCode());
System.out.println("getResponseMessage -> " + con.getResponseMessage());
// Map<String, String> test = new HashMap<String, String>();
test.put("cid", "TC0ONETIME");
test.put("partner_order_id", "0000000001"); //11자 테스트
test.put("partner_user_id", "pks1606");
test.put("item_name", "test");
test.put("item_code", "CB00001");
test.put("quantity", 1);
test.put("total_amount", 1);
test.put("tax_free_amount", 0);
// test.put("vat_amount", 1000); //필수 유무 확인
test.put("approval_url", "http://wdev.officeplus.com/order/payment/kakaoPayTest.jsp");
test.put("cancel_url", "http://wdev.officeplus.com/order/payment/kakaoPayTest.jsp");
test.put("fail_url", "http://wdev.officeplus.com/order/payment/kakaoPayTest.jsp");
String jsonInputString = test.toString();
System.out.println("JSON -> \n " + jsonInputString);
System.out.println("\n RESPONSE ->\n " + returnResponse(con, jsonInputString));
} catch (Exception e) {
System.out.println(e);
}
System.out.println("SUCCESS");
}
public String returnResponse(HttpURLConnection con, String jsonInputString) {
try (OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
} catch (Exception e) {
}
try {
System.out.println("flag1 -> " + con.getInputStream());
} catch (IOException e1) {
System.out.println("e1 -> " + e1.getMessage());
System.out.println("e1 -> " + e1.hashCode());
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
System.out.println("flag 2 -> " + br);
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
return response.toString();
} catch (Exception e) {
System.out.println("Couldnt read response from URL");
System.out.println(e);
return null;
}
}