package kr.co.swh.lecture.opensource.naver; 

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

// 네이버 캡차 API 예제 - 캡차 이미지 수신
/**
 * <pre>
 * kr.co.swh.lecture.opensource.naver 
 * APIExamCaptchaImage.java
 *
 * 설명 :
 * </pre>
 * 
 * @since : 2018. 10. 30.
 * @author : tobby48
 * @version : v1.0
 */
public class APIExamCaptchaImage {

	public static void main(String[] args) {
		String clientId = "OJgN42xxZiJXpnZtCH1j";//애플리케이션 클라이언트 아이디값";
		String clientSecret = "crXggqJhhW";//애플리케이션 클라이언트 시크릿값";
		try {
			String key = "EU0IbyyGtF67hXR0"; // https://openapi.naver.com/v1/captcha/nkey 호출로 받은 키값
			String apiURL = "https://openapi.naver.com/v1/captcha/ncaptcha.bin?key=" + key;
			URL url = new URL(apiURL);
			HttpURLConnection con = (HttpURLConnection)url.openConnection();
			con.setRequestMethod("GET");
			con.setRequestProperty("X-Naver-Client-Id", clientId);
			con.setRequestProperty("X-Naver-Client-Secret", clientSecret);
			int responseCode = con.getResponseCode();
			BufferedReader br;
			if(responseCode==200) { // 정상 호출
				InputStream is = con.getInputStream();
				int read = 0;
				byte[] bytes = new byte[1024];
				// 랜덤한 이름으로  파일 생성
				String tempname = Long.valueOf(new Date().getTime()).toString();
				File f = new File("tempname" + ".jpg");
				f.createNewFile();
				OutputStream outputStream = new FileOutputStream(f);
				while ((read =is.read(bytes)) != -1) {
					outputStream.write(bytes, 0, read);
				}
				is.close();
			} else {  // 에러 발생
				br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
				String inputLine;
				StringBuffer response = new StringBuffer();
				while ((inputLine = br.readLine()) != null) {
					response.append(inputLine);
				}
				br.close();
				System.out.println(response.toString());
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}