公司有直播的需求,选择了对接腾讯的sdk(吐槽一下,腾讯的文档写得是真的垃圾,但是功能倒是挺全的)
人脸核身:
用户首次使用时需要申请直播的开播权限,腾讯刚好有人脸核身的服务,需要在腾讯平台申请信息
由于我们使用的是appsdk 所以需要看的是appsdk 增强版的文档
大概流程是需要由服务端生成并下发参数,客户端获取参数后启动sdk,进行人脸核身,sdk返回人脸核身结果,服务端验证人脸结果
代码:
人脸核身sdk的信息,也就是申请sdk时的信息
@Component
@ConfigurationProperties(prefix = "tencent-faceid")
public class TencentFaceIdInfo {
// 腾讯人脸核身的访问api
private String bizTokenUrl;
//腾讯人脸核身sdkwbappid
private String WBappid;
//腾讯人脸核身sdk Secret
private String secret;
private String keyLicense;
}
Nacos 上的配置
人脸核身工具类
/**
* @author hzl
* 人脸核身
*/
@Component
@Slf4j
public class TencentFaceIdClient {
@Resource
private TencentFaceIdInfo tencentFaceIdInfo;
@Resource
private TencentLiveInfo tencentLiveInfo;
private static String ACCESS_TOKEN_URL = "https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token";
private static String TICKET_URL = "https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket";
private static String SUCCESS = "0";
/**
* 获取accessToken
* @return
* @throws Exception
*/
public String getAccessToken() throws Exception {
HashMap<String,Object> paramMap = new HashMap<>();
paramMap.put("app_id",tencentFaceIdInfo.getWBappid());
paramMap.put("secret",tencentFaceIdInfo.getSecret());
paramMap.put("grant_type","client_credential");
paramMap.put("version","1.0.0");
String result = HttpUtil.get(ACCESS_TOKEN_URL, paramMap);
JSONObject object = JSONObject.parseObject(result);
String code = object.getString("code");
if (!SUCCESS.equals(code)){
throw new BizException("请求AccessToken失败");
}
return object.getString("access_token");
}
/**
* 获取SignTicket
* @return
* @throws Exception
*/
public String getSignTicket(String accessToken) throws Exception {
HashMap<String,Object> paramMap = new HashMap<>();
paramMap.put("app_id",tencentFaceIdInfo.getWBappid());
paramMap.put("access_token",accessToken);
paramMap.put("type","SIGN");
paramMap.put("version","1.0.0");
String result = HttpUtil.get(TICKET_URL, paramMap);
JSONObject object = JSONObject.parseObject(result);
String code = object.getString("code");
if (!SUCCESS.equals(code)){
throw new BizException("请求SignTicket失败");
}
return object.getJSONArray("tickets").getJSONObject(0).