使用API模式接入

API模式即纯服务端模式或服务端API模式。ZOLOZ SaaS API独立于语言,因此您可以将API与任何语言集成。

在使用API模式接入ZOLOZ之前,您需要充分了解ZOLOZ网关协议以便正确创建API请求和处理响应;如果您的项目采用的是Java语言来编写,您可以利用现有的API SDK接入API。本文以Java API SDK为例,为您演示API接入流程。

前提条件

您已完成接入前的准备工作,请参见接入前准备

支持的产品

API接入模式支持以下产品:

  • Face Compare
  • ID Recognition
  • IdNetwork
  • 中国公民出入境记录识别

操作步骤

  1. 引入API SDK。
    在项目的POM文件中添加以下依赖项,将库引入项目中。
copy
<dependency>
    <groupId>com.zoloz.api.sdk</groupId>
    <artifactId>zoloz-api-sdk</artifactId>
    <version>0.1.0</version>
</dependency>
  1. 实例化并配置API SDK客户端。
copy
// initialize OpenApiClient
String clientId = "<Client ID>";
String zolozPublicKey = "<ZOLOZ's public key content encoded in base64>";
String merchantPrivateKey = "<The merchant's private key content encoded in base64>";

OpenApiClient client = new OpenApiClient();  // construct with signature and encryption by default
client.setHostUrl("https://sg-production-api.zoloz.com");
client.setClientId(clientId);
client.setMerchantPrivateKey(merchantPrivateKey);
client.setOpenApiPublicKey(zolozPublicKey);
//client.setSigned(false);     // signature (of response) validation can be turned off
//client.setEncrypted(false);  // encryption can be turned off

您需要将代码中的以下字段替换成您的真实信息。如需获取clientId、zolozPublicKey、merchantPrivateKey,请参见获取API凭证

    • clientId:客户ID。
    • zolozPublicKey:ZOLOZ交易公钥,采用Base64编码格式。
    • merchantPrivateKey:商户交易私钥,采用Base64编码格式
    • setHostUrl:ZOLOZ网关URL,如需获取ZOLOZ网关URL,请参见选择站点和环境
  1. 对应产品的API接入示例。
    • Face Compare
copy
// create api client
FaceCompareAPI faceCompareApi = new FaceCompareAPI(client);

// prepare api request
String face1ImgPath = "<file path of 1st face image>";
String face2ImgPath = "<file path of 2nd face image>";

FaceCompareRequest request = new FaceCompareRequest();
request.setBizId("biz-id-12345");  // for tracing purpose, it is recommended to use a global unique id
request.getFace1().setContent(getBase64ImageContent(face1ImgPath));
request.getFace2().setContent(getBase64ImageContent(face2ImgPath));

// call api
FaceCompareResponse response = faceCompareApi.compare(request);

if ("S".equals(response.getResult().getResultStatus())) {
    System.out.println(String.format(
            "Two faces are from %s, the similarity score is %2f",
            response.getSamePerson() ? "same person" : "different persons",
            response.getScore()
    ));
}
else {
    System.out.println(String.format(
            "[Error] %s: %s",
            response.getResult().getResultCode(),
            response.getResult().getResultMessage()
    ));
}
    • ID Recognition
copy
// create api client
DocRecognitionAPI docRecognitionAPI = new DocRecognitionAPI(client);

// prepare api request
String imagePath = cmd.getOptionValue("f");

DocRecognitionRequest request=new DocRecognitionRequest();
request.setBizId("biz-id-12345");  // for tracing purpose, it is recommended to use a global unique id
request.setDocType("00000001003");
request.setFrontPageImage(getBase64ImageContent(imagePath));

// call api
DocRecognitionResponse response = docRecognitionAPI.recognition(request);
if ("S".equals(response.getResult().getResultStatus())) {
    if ("Y".equals(response.getRecognitionResult())) {
        System.out.println("ID detected.\n");

        if (response.getSpoofResult() != null && !response.getSpoofResult().isEmpty()) {
            System.out.println("Spoofing Detection:");
            response.getOcrResult().forEach((key, value) -> {
                System.out.println(String.format(" -%s: %s", key, value));
            });

        }

        System.out.println("OCR Result:");
        response.getOcrResult().forEach((key, value) -> {
            System.out.println(String.format(" -%s: %s", key, value));
        });
        System.out.println(String.format(
                "[Error] %s: %s",
                response.getResult().getResultCode(),
                response.getResult().getResultMessage()
        ));
    }
    else {
        System.out.println(String.format(
                "Cannot recognize image: %s",
                response.getRecognitionErrorCode()
        ));
    }
}
else {
    System.out.println(String.format(
            "[Error] %s: %s",
            response.getResult().getResultCode(),
            response.getResult().getResultMessage()
    ));
}

代码示例

您可以在Github中获取开源的代码示例:

相关API