package kosta.web.mvc.controller;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@RestController
public class SelfCertificationController {
public static final String IMPORT_TOKEN_URL = "https://api.iamport.kr/users/getToken";
public static final String IMPORT_REQUEST_URL = "https://api.iamport.kr/certifications/******?_token=";
public static final String KEY = "*********";
public static final String SECRET = "*********";
/**
* 아임포트 인증(토큰)을 받아주는 함수
*/
public String getImportToken() {
String result = "";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(IMPORT_TOKEN_URL);
Map<String,String> m =new HashMap<String,String>();
m.put("imp_key", KEY);
m.put("imp_secret", SECRET);
try {
post.setEntity(new UrlEncodedFormEntity(convertParameter(m)));
HttpResponse res = client.execute(post);
ObjectMapper mapper = new ObjectMapper();
String body = EntityUtils.toString(res.getEntity());
JsonNode rootNode = mapper.readTree(body);
JsonNode resNode = rootNode.get("response");
result = resNode.get("access_token").asText();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// Map을 사용해서 Http요청 파라미터를 만들어 주는 함수
private List<NameValuePair> convertParameter(Map<String,String> paramMap){
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
Set<Map.Entry<String,String>> entries = paramMap.entrySet();
for(Map.Entry<String,String> entry : entries) {
paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
return paramList;
}
// 본인인증을 한 회원 정보를 받는 함수
@RequestMapping("/certifications")
public String getCertifications(String imp_uid) {
String birthday = "";
HttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(IMPORT_REQUEST_URL+imp_uid);
get.setHeader("Authorization", getImportToken());
try {
HttpResponse res = client.execute(get);
ObjectMapper mapper = new ObjectMapper();
String body = EntityUtils.toString(res.getEntity());
JsonNode rootNode = mapper.readTree(body);
JsonNode resNode = rootNode.get("response");
birthday = resNode.get("birthday").asText();
System.out.println("***************************************");
System.out.println("birthday = "+birthday);
System.out.println("***************************************");
} catch (Exception e) {
e.printStackTrace();
}
return birthday;
}
@RequestMapping("/aa")
public void aaa(String imp_uid) {
System.out.println(imp_uid);
}
@RequestMapping("/self")
public void self() {
System.out.println("*****************************************");
System.out.println("SelfCertificationController 의 self 영역");
System.out.println("*****************************************");
}
}