腾讯云人脸核身 服务端接入
腾讯云人脸核身 服务端接入
腾讯云人脸核身 服务端接入
引入SDK
微信 HTML5 接入
相关代码
接入步骤
引入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>
1
2
3
4
5
微信 HTML5 接入
微信 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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
然后封装一下接口,给前端调用即可.
————————————————
版权声明:本文为CSDN博主「lulin1992210」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lulin1992210/article/details/107534515