使用简单一段代码给注册用户进行腾讯云身份证实名认证API
在用户注册时需要对用户身份进行实名认证。为实现要求腾讯云市场提供该项服务,认证组合形式:【姓名、身份证号码】,且支持【港澳台居民在大陆办理的居住证】的验证 首先点击:[腾讯云【身份证实名认证接口】](https://market.cloud.tencent.com/products/9070)免费购买测试订单后在[控制台](https://console.cloud.tencent.com/servicemarket/services/market-evib2qie6)中可以得到您的appcode(密钥),服务页面有详细说明。 发送数据: ```java // 发送方式为post bodys.put("idNo", "340421190210182345"); bodys.put("custName", "张三"); ``` 返回数据: ```java { "name": "张三", "idNo": "340421190710145412", "respMessage": "身份证信息匹配", "respCode": "10000", "province": "安徽省", "city": "淮南市", "county": "凤台县", "birthday": "19071014", "sex": "M", "age": "111" } ``` 接口代码 具体实现类(其他语言版本在产品页面有详细介绍): ```javascript import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; import java.util.UUID; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import okhttp3.Call; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import sun.misc.BASE64Encoder; public class tengxunTest { public static String calcAuthorization(String secretId, String secretKey, String datetime) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { String signStr = "x-date: " + datetime; Mac mac = Mac.getInstance("HmacSHA1"); Key sKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), mac.getAlgorithm()); mac.init(sKey); byte[] hash = mac.doFinal(signStr.getBytes("UTF-8")); String sig = new BASE64Encoder().encode(hash); String auth = "{\"id\":\"" + secretId + "\", \"x-date\":\"" + datetime + "\", \"signature\":\"" + sig + "\"}"; return auth; } /**依赖的工具包有:okhttp-3.2.0.jar, okio-1.14.0.jar * 工具包下载链接:https://download.csdn.net/download/ruidongjun007/88360015 * * com.squareup.okhttp3 * okhttp * 3.2.0 * * * * com.squareup.okio * okio * 1.14.0 * */ public static String checkID(String name, String idno) throws IOException, InvalidKeyException, NoSuchAlgorithmException { //云市场分配的密钥Id String secretId = "AKxxxxxxxxxxxxxxx2qxo"; //云市场分配的密钥Key String secretKey = "4w1kxxxxxxxxxxxx8GDwY"; Calendar cd = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); String datetime = sdf.format(cd.getTime()); // 签名 String auth = calcAuthorization(secretId, secretKey, datetime); // 请求头 String uuid = UUID.randomUUID().toString(); // url String url = "http://ap-shanghai.cloudmarket-apigw.com/service-nktsmm99/idcheck"; String result = ""; RequestBody formBody = new FormBody.Builder().add("custName", name).add("idNo", idno).build(); Request request = new Request.Builder().url(url) .addHeader("request-id", uuid).addHeader("Authorization", auth).post(formBody).build(); Call call = new OkHttpClient().newCall(request); Response response = null; try { response = call.execute(); } catch (IOException e) { System.out.println("execute failed, message:" + e.getMessage()); } assert response != null; if (!response.isSuccessful()) { // 注意:续购不会改变秘钥(secretId、secretKey),仅增加可用次数 // 续购链接:https://market.cloud.tencent.com/products/9070 // 也可以加V【13451635131】咨询,共同进步! System.out.println("request failed----" + "返回状态码" + response.code() + ",message:" + response.message()); } result = response.body().string(); /**结果示例: * { "name": "张三", "idNo": "340421190710145412", "respMessage": "身份证信息匹配", "respCode": "10000", "province": "安徽省", "city": "淮南市", "county": "凤台县", "birthday": "19071014", "sex": "M", "age": "111" } */ /**respCode解释 * 返回码 返回信息 描述 10000 身份证信息匹配 身份证信息匹配 10001 开户名不能为空 开户名不能为空 10002 开户名不能包含特殊字符 开户名不能包含特殊字符 10003 身份证号不能为空 身份证号不能为空 10004 身份证号格式错误 身份证号格式错误 10005 姓名错误 姓名错误 10007 无此身份证号码 该身份证号码不存在 10008 身份证信息不匹配 身份证信息不匹配(姓名或身份证号码错误) 10010 系统维护,请稍后再试 系统维护,请稍后再试(维护前会短信和邮件通知 ,请留意通知信息) */ return result; } } ```