本文介绍了金融级实人认证纯服务端API的Python SDK调用示例。

SDK使用说明

如果需要其他语言的SDK示例代码,您可以通过OpenAPI 开发者门户在线调试工具调试API接口,该工具会自动生成相应API的SDK调用示例代码。您可以单击以下接口链接进行调试:

调试ContrastFaceVerify接口

前提条件

  • 需要使用的Python版本在3.0版本及以上。
  • 已添加认证场景,并获取对应的场景ID。具体操作,请参见添加认证场景

引入依赖包

pip install alibabacloud_tea_openapi==0.2.5

安装SDK

关于Python SDK源代码,请参见Python SDK包

pip install alibabacloud_cloudauth20190307==2.0.3

照片实人认证调用示例

关于参数的具体描述,请参见照片实人认证

from alibabacloud_cloudauth20190307.client import Client as CloudauthClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_cloudauth20190307 import models as cloudauth_models
from alibabacloud_tea_util.client import Client as UtilClient
from alibabacloud_tea_util import models as util_models


class ContrastFaceVerify:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
            endpoint: str,
    ) -> CloudauthClient:
        config = open_api_models.Config(
            # 您的AccessKey ID。
            access_key_id='您的AccessKey ID',
            # 您的AccessKey Secret。
            access_key_secret='您的AccessKey Secret',
            # 设置HTTP代理。
            # http_proxy='http://xx.xx.xx.xx:xxxx',
            # 设置HTTPS代理。
            # https_proxy='https://username:password@xxx.xxx.xxx.xxx:9999',
            endpoint=endpoint
        )
        return CloudauthClient(config)

    @staticmethod
    def main() -> None:
        request = cloudauth_models.ContrastFaceVerifyRequest(
            # 请输入场景ID。
            scene_id=0,
            outer_order_no='商户请求的唯一标识',
            # 要接入的认证方案。
            product_code='ID_MIN',
            model='NO_LIVENESS',
            # 固定值IDENTITY_CARD。
            cert_type='IDENTITY_CARD',
            cert_name='xxx',
            cert_no='xxx',
            # ip='114.xxx.xxx.xxx',
            # user_id='用户ID',
            # mobile='130xxxxxxxx',
            # device_token='通过设备指纹获取的deviceToken',

            # 请在以下四种方式中选择其一。
            # 1.照片Base64编码。
            # 2.照片OSS bucket名和文件名。
            # 3.照片OSS的URL地址。
            # 4.已经实人认证通过的CertifyId。

            # 方式一:照片Base64。
            # face_contrast_picture='人脸照片Base64编码'
            # 方式二:照片OSS bucket名和文件名。
            # oss_bucket_name='cn-shanghai-aliyun-cloudauth-xxxxx',
            # oss_object_name='verify/xxxxx/xxxxxx.jpeg'
            # 方式三:照片OSS的URL地址,公网可访问。
            face_contrast_picture_url='https://cn-shanghai-aliyun-cloudauth-xxxxxx.oss-cn-shanghai.aliyuncs.com/verify/xxxxx/xxxxx.jpeg'
            # 方式四:已经实人认证通过的CertifyId。
            # certify_id='0bfa7c493f850e5178b9f8613634c9xx',

            # 如需开启个人信息加密传输。
            # encrypt_type='SM2',
            # cert_name='BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==',
            # cert_no='BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg=='
        )
        response = ContrastFaceVerify.contrast_face_verify_auto_route(request)
        # 不支持服务自动路由。
        # response = ContrastFaceVerify.contrast_face_verify('cloudauth.cn-shanghai.aliyuncs.com', request)
        print(response.body.request_id)
        print(response.body.code)
        print(response.body.message)
        print(response.body.result_object.passed)
        print(response.body.result_object.identity_info)
        print(response.body.result_object.sub_code)
        print(response.body.result_object.material_info)
        print(response.body.result_object.certify_id)

    @staticmethod
    def contrast_face_verify_auto_route(
            request: cloudauth_models.ContrastFaceVerifyRequest,
    ) -> cloudauth_models.ContrastFaceVerifyResponse:
        endpoints = [
            'cloudauth.cn-shanghai.aliyuncs.com',
            'cloudauth.cn-beijing.aliyuncs.com'
        ]
        for endpoint in endpoints:
            try:
                response = ContrastFaceVerify.contrast_face_verify(endpoint, request)
                if UtilClient.equal_number(500, response.status_code):
                    continue
                if response is not None and response.body is not None:
                    if UtilClient.equal_string("500", response.body.code):
                        continue
                return response
            except Exception as err:
                print(err)
                continue
        return None

    @staticmethod
    def contrast_face_verify(
            endpoint: str,
            request: cloudauth_models.ContrastFaceVerifyRequest,
    ) -> cloudauth_models.ContrastFaceVerifyResponse:
        client = ContrastFaceVerify.create_client(endpoint)
        # 创建RuntimeObject实例并设置运行参数。
        runtime = util_models.RuntimeOptions()
        runtime.read_timeout = 10000
        runtime.connect_timeout = 10000
        return client.contrast_face_verify_with_options(request, runtime)


if __name__ == '__main__':
    ContrastFaceVerify.main()
            

视频实人认证调用示例

支持OSS对象存储方式和本地文件两种方式。关于参数的具体描述,请参见视频实人认证

使用本地文件的方式(推荐)

from alibabacloud_cloudauth20190307.client import Client as CloudauthClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_cloudauth20190307 import models as cloudauth_models
from alibabacloud_tea_util.client import Client as UtilClient
from alibabacloud_tea_util import models as util_models


class ContrastFaceVerifyVideo:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
            endpoint: str,
    ) -> CloudauthClient:
        config = open_api_models.Config(
            # 您的AccessKey ID。
            access_key_id='您的AccessKey ID',
            # 您的AccessKey Secret。
            access_key_secret='您的AccessKey Secret',
            # 设置HTTP代理。
            # http_proxy='http://xx.xx.xx.xx:xxxx',
            # 设置HTTPS代理。
            # https_proxy='https://username:password@xxx.xxx.xxx.xxx:9999',
            endpoint=endpoint
        )
        return CloudauthClient(config)

    @staticmethod
    def main() -> None:
        file = open('本地文件路径', 'rb')
        request = cloudauth_models.ContrastFaceVerifyAdvanceRequest(
            # 请输入场景ID。
            scene_id=0,
            outer_order_no='商户请求的唯一标识',
            # 要接入的认证方案。
            product_code='ID_MIN_VIDEO',
            model='FRONT_CAMERA_LIVENESS',
            # 固定值IDENTITY_CARD。
            cert_type='IDENTITY_CARD',
            cert_name='xxx',
            cert_no='xxx',
            # ip='114.xxx.xxx.xxx',
            # user_id='用户ID',
            # mobile='130xxxxxxxx',
            # device_token='通过设备指纹获取的deviceToken',
            face_contrast_file_object=file,
            # 如需开启个人信息加密传输。
            # encrypt_type='SM2',
            # cert_name='BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==',
            # cert_no='BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg=='
        )
        response = ContrastFaceVerifyVideo.contrast_face_verify_auto_route(request)
        # 不支持服务自动路由。
        # response = ContrastFaceVerify.contrast_face_verify('cloudauth.cn-shanghai.aliyuncs.com', request)
        print(response.body.request_id)
        print(response.body.code)
        print(response.body.message)
        print(response.body.result_object.passed)
        print(response.body.result_object.identity_info)
        print(response.body.result_object.sub_code)
        print(response.body.result_object.material_info)
        print(response.body.result_object.certify_id)

        file.close()

    @staticmethod
    def contrast_face_verify_auto_route(
            request: cloudauth_models.ContrastFaceVerifyAdvanceRequest,
    ) -> cloudauth_models.ContrastFaceVerifyResponse:
        endpoints = [
            'cloudauth.cn-shanghai.aliyuncs.com',
            'cloudauth.cn-beijing.aliyuncs.com'
        ]
        for endpoint in endpoints:
            try:
                response = ContrastFaceVerifyVideo.contrast_face_verify(endpoint, request)
                if UtilClient.equal_number(500, response.status_code):
                    continue
                if response is not None and response.body is not None:
                    if UtilClient.equal_string("500", response.body.code):
                        continue
                return response
            except Exception as err:
                print(err)
                continue
        return None

    @staticmethod
    def contrast_face_verify(
            endpoint: str,
            request: cloudauth_models.ContrastFaceVerifyAdvanceRequest,
    ) -> cloudauth_models.ContrastFaceVerifyResponse:
        client = ContrastFaceVerifyVideo.create_client(endpoint)
        # 创建RuntimeObject实例并设置运行参数。
        runtime = util_models.RuntimeOptions()
        runtime.read_timeout = 10000
        runtime.connect_timeout = 10000
        return client.contrast_face_verify_advance(request, runtime)


if __name__ == '__main__':
    ContrastFaceVerifyVideo.main()
            

使用OSS对象存储的方式

from alibabacloud_cloudauth20190307.client import Client as CloudauthClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_cloudauth20190307 import models as cloudauth_models
from alibabacloud_tea_util.client import Client as UtilClient
from alibabacloud_tea_util import models as util_models


class ContrastFaceVerifyVideoOSS:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
            endpoint: str,
    ) -> CloudauthClient:
        config = open_api_models.Config(
            # 您的AccessKey ID。
            access_key_id='您的AccessKey ID',
            # 您的AccessKey Secret。
            access_key_secret='您的AccessKey Secret',
            # 设置HTTP代理。
            # http_proxy='http://xx.xx.xx.xx:xxxx',
            # 设置HTTPS代理。
            # https_proxy='https://username:password@xxx.xxx.xxx.xxx:9999',
            endpoint=endpoint
        )
        return CloudauthClient(config)

    @staticmethod
    def main() -> None:
        request = cloudauth_models.ContrastFaceVerifyRequest(
            # 请输入场景ID。
            scene_id=0,
            outer_order_no='商户请求的唯一标识',
            # 要接入的认证方案。
            product_code='ID_MIN_VIDEO',
            model='FRONT_CAMERA_LIVENESS',
            # 固定值IDENTITY_CARD。
            cert_type='IDENTITY_CARD',
            cert_name='xxx',
            cert_no='xxx',
            # ip='114.xxx.xxx.xxx',
            # user_id='用户ID',
            # mobile='130xxxxxxxx',
            # device_token='通过设备指纹获取的deviceToken',
            oss_bucket_name='cn-shanghai-aliyun-cloudauth-xxxxx',
            oss_object_name='verify/xxxxx/xxxxxx.mp4',
            # 如需开启个人信息加密传输。
            # encrypt_type='SM2',
            # cert_name='BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==',
            # cert_no='BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg=='
        )
        response = ContrastFaceVerifyVideoOSS.contrast_face_verify_auto_route(request)
        # 不支持服务自动路由。
        # response = ContrastFaceVerify.contrast_face_verify('cloudauth.cn-shanghai.aliyuncs.com', request)
        print(response.body.request_id)
        print(response.body.code)
        print(response.body.message)
        print(response.body.result_object.passed)
        print(response.body.result_object.identity_info)
        print(response.body.result_object.sub_code)
        print(response.body.result_object.material_info)
        print(response.body.result_object.certify_id)

    @staticmethod
    def contrast_face_verify_auto_route(
            request: cloudauth_models.ContrastFaceVerifyRequest,
    ) -> cloudauth_models.ContrastFaceVerifyResponse:
        endpoints = [
            'cloudauth.cn-shanghai.aliyuncs.com',
            'cloudauth.cn-beijing.aliyuncs.com'
        ]
        for endpoint in endpoints:
            try:
                response = ContrastFaceVerifyVideoOSS.contrast_face_verify(endpoint, request)
                if UtilClient.equal_number(500, response.status_code):
                    continue
                if response is not None and response.body is not None:
                    if UtilClient.equal_string("500", response.body.code):
                        continue
                return response
            except Exception as err:
                continue
        return None

    @staticmethod
    def contrast_face_verify(
            endpoint: str,
            request: cloudauth_models.ContrastFaceVerifyRequest,
    ) -> cloudauth_models.ContrastFaceVerifyResponse:
        client = ContrastFaceVerifyVideoOSS.create_client(endpoint)
        # 创建RuntimeObject实例并设置运行参数。
        runtime = util_models.RuntimeOptions()
        runtime.read_timeout = 10000
        runtime.connect_timeout = 10000
        return client.contrast_face_verify_with_options(request, runtime)

if __name__ == '__main__':
    ContrastFaceVerifyVideoOSS.main()