如何使用身份证实名认证接口呢?充钱!充钱!充钱!
-
首先,去阿里云市场购买身份证实名认证服务。
-
购物之后,我们会得到:AppKey,AppSecret,AppCode
-
根据官方提供的实例代码,使用实名认证接口。我这里有一个自己测试过的代码,发生请求,用到的Spring提供的RestTemplate类。
/** * * @param realName 用户的真实姓名 * @param carNum 用户的身份证号码 * @return 验证的结果 */ public static boolean check(String realName, String carNum) { MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>(); paramMap.put("cardNo", Collections.singletonList(carNum)); paramMap.put("realName", Collections.singletonList(realName)); HttpHeaders headers = new HttpHeaders(); headers.put("Authorization", Collections.singletonList("APPCODE " + idProperties.getAppCode())); headers.put("Content-Type", Collections.singletonList("application/x-www-form-urlencoded; charset=UTF-8")); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers); ResponseEntity<String> response = restTemplate.exchange(idProperties.getUrl(), HttpMethod.POST, httpEntity, String.class); System.out.println(response.getStatusCode()); System.out.println(response.getBody()); if (response.getStatusCode() == HttpStatus.OK) { String body = response.getBody(); JSONObject jsonObject = JSON.parseObject(body); int staus = jsonObject.getInteger("error_code"); JSONObject result = jsonObject.getJSONObject("result"); Boolean isok = result.getBoolean("isok"); if (staus == 0 && isok) { return true; } } return false; }