本文介绍了增强版活体检测方案的Java SDK调用示例。

SDK使用说明

如果需要其他语言的SDK示例代码,您可以通过 OpenAPI开发者门户在线调试工具调试API接口。该工具会自动生成相应API的SDK调用示例代码,详细信息,请参见:

前提条件

使用Maven引入SDK

<dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <version>4.5.3</version>
</dependency>
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-cloudauth</artifactId>
  <version>2.0.20</version>
</dependency>

调用示例

InitSmartVerify接口调用示例

import java.util.Arrays;
import java.util.List;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.cloudauth.model.v20200618.InitSmartVerifyRequest;
import com.aliyuncs.cloudauth.model.v20200618.InitSmartVerifyResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.http.HttpClientType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.regions.ProductDomain;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.3</version>
</dependency>
*/
public class InitSmartVerify {
    public static void main(String[] args) throws ClientException {

        InitSmartVerifyRequest request = new InitSmartVerifyRequest();
        // 请输入SceneId+L。
        request.setSceneId(xxxxL);

        request.setOuterOrderNo("商户请求的唯一标识");
        // 卡证核身类型,固定值。
        request.setMode("ACCOUNT_SAFE");
        // 证件类型,固定值。
        request.setCertType("IDENTITY_CARD");
        //request.setCertName("张三");
        //request.setCertNo("xxxx");
        // MetaInfo环境参数,需要通过客户端SDK获取。
        request.setMetaInfo("xxxxx");
        //request.setMobile("130xxxxxxxx");
        //request.setIp("114.xxx.xxx.xxx");
        //request.setUserId("12345xxxx");
        //request.setCallbackUrl("https://www.xxxx.com");
        //request.setCallbackToken("xxxxx");

        List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com");

        // 推荐,支持服务路由。
        InitSmartVerifyResponse response = initSmartVerifyAutoRoute(endpoints, request);

        // 不支持服务自动路由。
        //InitSmartVerifyResponse response = initSmartVerify("cloudauth.cn-shanghai.aliyuncs.com", request);

        System.out.println(response.getCode());
        System.out.println(response.getMessage());
        System.out.println(response.getRequestId());
        System.out.println(response.getResultObject() == null ? null : response.getResultObject().getCertifyId());
    }

    private static InitSmartVerifyResponse initSmartVerifyAutoRoute(List<String> endpoints,
        InitSmartVerifyRequest request) {

        InitSmartVerifyResponse lastResponse = null;
        for (String endpoint : endpoints) {
            try {
                InitSmartVerifyResponse response = initSmartVerify(endpoint, request);
                lastResponse = response;
                if ("500".equals(response.getCode())) {
                    continue;
                }

                return response;
            } catch (ClientException e) {
                if ("ServiceUnavailable".equals(e.getErrCode()) || "SDK.ServerUnreachable".equals(e.getErrCode())
                    || "SDK.ReadTimeout".equals(e.getErrCode())) {
                    continue;
                }

                System.out.println(e.getErrCode());
                System.out.println(e.getErrMsg());
                System.out.println(e.getRequestId());
            }
        }

        return lastResponse;
    }

    private static InitSmartVerifyResponse initSmartVerify(String endpoint, InitSmartVerifyRequest request)
        throws ClientException {
        // Client代理配置。
        HttpClientConfig config = new HttpClientConfig();
        config.setClientType(HttpClientType.ApacheHttpClient);
        //config.setProtocolType(ProtocolType.HTTPS);
        //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
        //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
        //config.setNoProxy("xx.xx.xx.xx,localhost");
        config.setConnectionTimeoutMillis(10000);
        config.setReadTimeoutMillis(10000);
        DefaultProfile profile = DefaultProfile.getProfile("", "您的AccessKey ID",
            "您的AccessKey Secret");
        profile.setHttpClientConfig(config);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        request.setSysProductDomain(new ProductDomain("cloudauth", endpoint));

        return client.getAcsResponse(request);
    }
}

DescribeSmartVerify接口调用示例

import java.util.Arrays;
import java.util.List;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.cloudauth.model.v20200618.DescribeSmartVerifyRequest;
import com.aliyuncs.cloudauth.model.v20200618.DescribeSmartVerifyResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.http.HttpClientType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.regions.ProductDomain;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.3</version>
</dependency>
*/
public class DescribeSmartVerify {
    public static void main(String[] args) throws ClientException {

        DescribeSmartVerifyRequest request = new DescribeSmartVerifyRequest();
        // 请输入SceneId+L。
        request.setSceneId(xxxxL);

        request.setCertifyId("xxxx");

        List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com");

        // 推荐,支持服务路由。
        DescribeSmartVerifyResponse response = describeSmartVerifyAutoRoute(endpoints, request);

        // 不支持服务自动路由。
        //DescribeSmartVerifyResponse response = describeSmartVerify("cloudauth.cn-shanghai.aliyuncs.com", request);

        System.out.println(response.getCode());
        System.out.println(response.getMessage());
        System.out.println(response.getRequestId());
        System.out.println(response.getResultObject() == null ? null : response.getResultObject().getPassed());
        System.out.println(response.getResultObject() == null ? null : response.getResultObject().getSubCode());
        System.out.println(response.getResultObject() == null ? null : response.getResultObject().getMaterialInfo());
    }

    private static DescribeSmartVerifyResponse describeSmartVerifyAutoRoute(List<String> endpoints,
        DescribeSmartVerifyRequest request) {

        DescribeSmartVerifyResponse lastResponse = null;
        for (String endpoint : endpoints) {
            try {
                DescribeSmartVerifyResponse response = describeSmartVerify(endpoint, request);
                lastResponse = response;
                if ("500".equals(response.getCode())) {
                    continue;
                }

                return response;
            } catch (ClientException e) {
                if ("ServiceUnavailable".equals(e.getErrCode()) || "SDK.ServerUnreachable".equals(e.getErrCode())
                    || "SDK.ReadTimeout".equals(e.getErrCode())) {
                    continue;
                }

                System.out.println(e.getErrCode());
                System.out.println(e.getErrMsg());
                System.out.println(e.getRequestId());
            }
        }

        return lastResponse;
    }

    private static DescribeSmartVerifyResponse describeSmartVerify(String endpoint, DescribeSmartVerifyRequest request)
        throws ClientException {
        // Client代理配置。
        HttpClientConfig config = new HttpClientConfig();
        config.setClientType(HttpClientType.ApacheHttpClient);
        //config.setProtocolType(ProtocolType.HTTPS);
        //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
        //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
        //config.setNoProxy("xx.xx.xx.xx,localhost");
        config.setConnectionTimeoutMillis(10000);
        config.setReadTimeoutMillis(10000);
        DefaultProfile profile = DefaultProfile.getProfile("", "您的AccessKey ID",
            "您的AccessKey Secret");
        profile.setHttpClientConfig(config);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        request.setSysProductDomain(new ProductDomain("cloudauth", endpoint));

        return client.getAcsResponse(request);
    }
}