在用户注册时需要对用户身份进行实名认证。为实现要求阿里云市场提供该项服务,认证组合形式:【姓名、身份证号码】,且支持【港澳台居民在大陆办理的居住证】的验证
首先点击:阿里云【身份证实名认证接口】免费购买测试订单后在控制台中可以得到您的appcode(密钥),服务页面有详细说明。
数据类型
发送数据:
// 发送方式为post
bodys.put("idNo", "340421190210182345");
bodys.put("name", "张三");
返回数据:
// 返回的数据类型为json
{
"name": "张三",
"idNo": "340421190710145412",
"respMessage": "身份证信息匹配",
"respCode": "0000",
"province": "安徽省",
"city": "淮南市",
"county": "凤台县",
"birthday": "19071014",
"sex": "M",
"age": "111"
}
接口代码
具体实现类(其他语言版本在产品页面有详细介绍):
import java.io.IOException;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class idAuthDemo {
public static void main(String[] args) throws IOException {
String url = "https://idenauthen.market.alicloudapi.com/idenAuthentication";
// 获取appCode链接:https://market.aliyun.com/detail/cmapi025518 下入试用包后在控制台查看
String appCode = "e1ff33s21dfg2s1dd2f1ff33fc60d7130";
String name = "张三";
String idNo = "320000198811110000";
System.out.println(postData(appCode, url, name, idNo));
/** 错误码respCode信息:
* "0000": "身份证信息匹配成功",
* "0001": "开户名不能为空",
* "0002": "开户名不能包含特殊字符",
* "0003": "身份证号不能为空",
* "0004": "身份证号格式错误",
* "0007": "无此身份证号码(该身份证号码不存在)",
* "0008": "身份证信息不匹配(如姓名与身份证号不匹配)",
* "0010": "系统维护,请稍后再试(维护前会短信和邮件通知,请留意通知信息)"
*/
}
/**依赖的工具包有:okhttp-3.2.0.jar, okio-1.14.0.jar
* 工具包下载链接:https://download.csdn.net/download/ruidongjun007/88360015
* <dependency>
* <groupId>com.squareup.okhttp3</groupId>
* <artifactId>okhttp</artifactId>
* <version>3.2.0</version>
* </dependency>
*
* <dependency>
* <groupId>com.squareup.okio</groupId>
* <artifactId>okio</artifactId>
* <version>1.14.0</version>
* </dependency>
*/
public static String postData(String appCode, String url, String name, String idNo) throws IOException {
String result = "";
RequestBody formBody = new FormBody.Builder().
add("name", name).add("idNo", idNo).build();
Request request = new Request.Builder().url(url).
addHeader("Authorization", "APPCODE " + appCode).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()) {
// 状态码为403时一般是套餐包用尽,需续购;
// 注意:续购不会改变秘钥(appCode),仅增加次数
// 续购链接:https://marketnext.console.aliyun.com/bizlist
// 也可以加V【13451635131】咨询,共同进步!
System.out.println("request failed----" + "返回状态码" + response.code() +
",message:" + response.message());
}
result = response.body().string();
/** 结果示例:
* {
* "name": "张三",
* "idNo": "340421190710145412",
* "respMessage": "身份证信息匹配",
* "respCode": "0000",
* "province": "安徽省",
* "city": "淮南市",
* "county": "凤台县",
* "birthday": "19071014",
* "sex": "M",
* "age": "111"
* }
*/
return result;
}
}