腾讯云人脸核身 服务端接入
接入步骤
引入SDK
Tencent Cloud SDK 3.0 for Java
安装方式我选择的通过 Maven 安装,pom.xml引入即可
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.96</version>
</dependency>
微信 HTML5 接入
我这里使用的微信 H5 普通模式,具体流程看链接.
服务端只需要封装两个接口给前端调用,实名核身鉴权 DetectAuth 接口,获取实名核身结果信息 GetDetectInfo.
相关代码
import com.alibaba.fastjson.JSONObject;
import com.sjb.smartlaborcommon.util.BASE64Util;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthRequest;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthResponse;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoRequest;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoResponse;
import lombok.extern.slf4j.Slf4j;
/**
* 腾讯云SDK 微信普通H5
*
* 接入方服务端调用实名核身鉴权 DetectAuth 接口,传入核身所需信息与业务回跳地址 RedirectUrl,获取到核身流程标识(BizToken)及核身入口 URL
* 接入方前端通过地址跳转方式重定向至步骤3中获取的核身入口 URL,进入核身流程。
* 用户完成人脸核身后,页面会跳转到 RedirectUrl 上,地址中会带上此次验证流程使用的 BizToken,接入方服务端即可凭借 BizToken 参数调用获取实名核身结果信息 GetDetectInfo 接口去获取本次核身的详细信息。
*/
@Slf4j
public class TencentCloudWxUtil {
//------------------------测试--Begin-------------------------
//appId
private static String appId = "1302661795";
//个人密钥id
private static String secretId = "AKIDbvyjELN8l7F7i1npMo6lBKDpQoxdGsAE";
//个人密钥key
private static String secretKey = "xf6G9LJ4XTaeV6TZOwDMGvJTnqc8OPAi";
//业务id(用于细分客户使用场景)
private static String ruleId = "0";
//指定拉取的结果信息,取值(0:全部;1:文本类;2:身份证正反面;3:视频最佳截图照片;4:视频)
private static String InfoType = "0";
//------------------------测试--End-------------------------
/**
* @Title 实名核身鉴权
* @description
* @Param idCard(身份证 必填),name(姓名 必填),identityImage(身份证人像面url 必填)
* @author ll
* @return {"Url":"","BizToken":"","RequestId":""}
* @version 1.0
* @time 2020年7月23日 12:13:01
*/
public static DetectAuthResponse detectAuth(String idCard,String name,String identityImage) throws Exception{
DetectAuthResponse resp = new DetectAuthResponse();
try {
Credential cred = new Credential(secretId,secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("faceid.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
FaceidClient client = new FaceidClient(cred,"ap-nanjing",clientProfile);
//转为json字符串
JSONObject requestBody = new JSONObject();
requestBody.put("RuleId",ruleId);
requestBody.put("IdCard",idCard);
requestBody.put("Name",name);
String imgBase64IdentityImage = BASE64Util.imgBase64(identityImage);
requestBody.put("ImageBase64",imgBase64IdentityImage);
String params = JSONObject.toJSONString(requestBody);
DetectAuthRequest req = DetectAuthRequest.fromJsonString(params,DetectAuthRequest.class);
resp = client.DetectAuth(req);
log.info("返回结果:{}",DetectAuthResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return resp;
}
/**
* @Title 获取实名核身结果信息
* @description
* @Param bizToken(人脸核身流程的标识,调用DetectAuth接口时生成。 必填)
* @author lulin
* @version 1.0
* @time 2020年7月23日 12:13:01
*/
public static GetDetectInfoResponse getDetectInfo(String bizToken) throws Exception{
GetDetectInfoResponse resp = new GetDetectInfoResponse();
try {
Credential cred = new Credential(secretId,secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("faceid.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
FaceidClient client = new FaceidClient(cred,"ap-nanjing",clientProfile);
//转为json字符串
JSONObject requestBody = new JSONObject();
requestBody.put("RuleId",ruleId);
requestBody.put("BizToken",bizToken);
requestBody.put("InfoType",InfoType);
String params = JSONObject.toJSONString(requestBody);
GetDetectInfoRequest req = GetDetectInfoRequest.fromJsonString(params,GetDetectInfoRequest.class);
resp = client.GetDetectInfo(req);
log.info("返回结果:{}",GetDetectInfoResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return resp;
}
}
然后封装一下接口,给前端调用即可.