本文介绍了金融级活体检测方案Java SDK调用示例。

SDK使用说明

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

本章节提供的是最新版本的Java SDK调用示例,如果您需要查看之前版本,请参见Java SDK调用示例(V1版本)

前提条件

使用Maven引入SDK

<dependency>
       <groupId>com.aliyun</groupId>
       <artifactId>cloudauth20190307</artifactId>
        <version>1.0.1</version>
</dependency>

调用示例

InitFaceVerify接口调用示例

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

import com.aliyun.cloudauth20190307.Client;
import com.aliyun.cloudauth20190307.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.tearpc.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class InitFaceVerify {

    public static void main(String[] args) throws Exception {

        InitFaceVerifyRequest request = new InitFaceVerifyRequest();
        // 请输入场景ID+L。
        request.setSceneId(xxxxL);
        // 设置商户请求的唯一标识。
        request.setOuterOrderNo("xxxx");
        // 认证方案。
        request.setProductCode("LR_FR");
        request.setUserId("12345xxxx");
        // 模式。
        request.setModel("LIVENESS");
        //request.setCertType("IDENTITY_CARD");
        //request.setCertName("张三");
        //request.setCertNo("xxxx");
        // MetaInfo环境参数。
        request.setMetaInfo("xxxxxx");
        //request.setMobile("130xxxxxxxx");
        //request.setIp("114.xxx.xxx.xxx");

        // 推荐,支持服务路由。
        InitFaceVerifyResponse response = initFaceVerifyAutoRoute(request);

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

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

    private static InitFaceVerifyResponse initFaceVerifyAutoRoute(InitFaceVerifyRequest request) {
        // 第一个为主区域Endpoint,第二个为备区域Endpoint。
        List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com");
        InitFaceVerifyResponse lastResponse = null;
        for (String endpoint : endpoints) {
            try {
                InitFaceVerifyResponse response = initFaceVerify(endpoint, request);
                lastResponse = response;

                // 服务端错误,切换到下个区域调用。
                if (response != null && "500".equals(response.getCode())) {
                    continue;
                }

                return response;
            } catch (Exception e) {
                // 网络异常,切换到下个区域调用。
                if (e.getCause() instanceof TeaException) {
                    TeaException teaException = ((TeaException)e.getCause());
                    if (teaException.getData() != null && "ServiceUnavailable".equals(
                        teaException.getData().get("Code"))) {
                        continue;
                    }
                }

                if (e.getCause() instanceof TeaUnretryableException) {
                    continue;
                }
            }
        }

        return lastResponse;
    }

    private static InitFaceVerifyResponse initFaceVerify(String endpoint, InitFaceVerifyRequest request)
        throws Exception {
        Config config = new Config();
        config.setAccessKeyId("您的AccessKey ID");
        config.setAccessKeySecret("您的AccessKey Secret");
        config.setEndpoint(endpoint);
        // 设置http代理。
        //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
        // 设置https代理。
        //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
        Client client = new Client(config);

        // 创建RuntimeObject实例并设置运行参数。
        RuntimeOptions runtime = new RuntimeOptions();
        runtime.readTimeout = 10000;
        runtime.connectTimeout = 10000;

        return client.initFaceVerify(request, runtime);
    }
}

DescribeFaceVerify接口调用示例

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

import com.aliyun.cloudauth20190307.Client;
import com.aliyun.cloudauth20190307.models.DescribeFaceVerifyRequest;
import com.aliyun.cloudauth20190307.models.DescribeFaceVerifyResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.tearpc.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class DescribeFaceVerify {

    public static void main(String[] args) throws Exception {

        // 通过以下代码创建API请求并设置参数。
        DescribeFaceVerifyRequest request = new DescribeFaceVerifyRequest();
        // 请输入场景ID+L。
        request.setSceneId(xxxxL);

        request.setCertifyId("xxxx");

        // 推荐,支持服务路由。
        DescribeFaceVerifyResponse response = describeFaceVerifyAutoRoute(request);

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

        System.out.println(response.getRequestId());
        System.out.println(response.getCode());
        System.out.println(response.getMessage());
        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().getIdentityInfo());
        System.out.println(
            response.getResultObject() == null ? null
                : response.getResultObject().getDeviceToken());
        System.out.println(
            response.getResultObject() == null ? null
                : response.getResultObject().getMaterialInfo());
    }

    private static DescribeFaceVerifyResponse describeFaceVerifyAutoRoute(DescribeFaceVerifyRequest request) {
        // 第一个为主区域Endpoint,第二个为备区域Endpoint。
        List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com");
        DescribeFaceVerifyResponse lastResponse = null;
        for (String endpoint : endpoints) {
            try {
                DescribeFaceVerifyResponse response = describeFaceVerify(endpoint, request);
                lastResponse = response;

                // 服务端错误,切换到下个区域调用。
                if (response != null && "500".equals(response.getCode())) {
                    continue;
                }

                return response;
            } catch (Exception e) {
                // 网络异常,切换到下个区域调用。
                if (e.getCause() instanceof TeaException) {
                    TeaException teaException = ((TeaException)e.getCause());
                    if (teaException.getData() != null && "ServiceUnavailable".equals(
                        teaException.getData().get("Code"))) {
                        continue;
                    }
                }

                if (e.getCause() instanceof TeaUnretryableException) {
                    continue;
                }
            }
        }

        return lastResponse;
    }

    private static DescribeFaceVerifyResponse describeFaceVerify(String endpoint, DescribeFaceVerifyRequest request)
        throws Exception {
        Config config = new Config();
        config.setAccessKeyId("您的AccessKey ID");
        config.setAccessKeySecret("您的AccessKey Secret");
        config.setEndpoint(endpoint);
        // 设置http代理。
        //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
        // 设置https代理。
        //config.setHttpsProxy("http://xx.xx.xx.xx:xxxx");
        Client client = new Client(config);

        // 创建RuntimeObject实例并设置运行参数。
        RuntimeOptions runtime = new RuntimeOptions();
        runtime.readTimeout = 10000;
        runtime.connectTimeout = 10000;

        return client.describeFaceVerify(request, runtime);
    }
}