Python SDK调用示例

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

SDK使用说明

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

前提条件

  • 需要使用的Python版本在3.0版本及以上。

  • 已添加认证场景,并获取对应的场景ID。具体操作,请参见添加认证场景

引入依赖包

pip install alibabacloud_tea_openapi==0.2.5
pip install alibabacloud_credentials==0.3.2

安装SDK

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

pip install alibabacloud_cloudauth20200618==1.0.5

调用示例

关于参数的具体描述,请参见InitSmartVerifyDescribeSmartVerify

InitSmartVerify接口调用示例

import sys
from typing import List

from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_cloudauth20200618.client import Client as CloudauthClient
from alibabacloud_cloudauth20200618 import models as cloudauth_models
from alibabacloud_tea_util.client import Client as UtilClient
from alibabacloud_tea_util import models as util_models
from alibabacloud_credentials.client import Client as CredClient

class InitSmartVerify:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
            endpoint: str,
    ) -> CloudauthClient:
        # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        # 本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见 https://help.aliyun.com/zh/sdk/developer-reference/configure-credentials。
        cred=CredClient()
        config = open_api_models.Config(
            credential=cred,
            # 设置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(
            args: List[str],
    ) -> None:

        request = cloudauth_models.InitSmartVerifyRequest(
            # 请输入场景ID。
            scene_id=xxx,
            outer_order_no='商户请求的唯一标识',
            # 证件类型,固定值。
            mode='ACCOUNT_SAFE',
            cert_type='IDENTITY_CARD',
            # cert_name='张三',
            # cert_no='xxxxx',
            # MetaInfo环境参数,需要通过客户端SDK获取。
            meta_info='{"zimVer":"3.0.0","appVersion": "1","bioMetaInfo": "4.1.0:11501568,0","appName":"com.aliyun.cloudauth","deviceType": "ios","osVersion": "iOS 10.3.2","apdidToken": "","deviceModel": "iPhone9,1"}',
            mobile='130xxxxxxxx',
            # ip="114.xxx.xxx.xxx",
            # user_id="12345xxxx",
            # callback_url="https://www.aliyundoc.com",
            # callback_token="xxxxxxx"
        )
        response = InitSmartVerify.init_smart_verify_auto_route(request)
        # 不支持服务自动路由。
        # response = InitSmartVerify.init_smart_verify('cloudauth.cn-shanghai.aliyuncs.com', request)
        if response is not None:
            print(response.request_id)
            print(response.code)
            print(response.message)
        if response.result_object is not None:
            print(response.result_object.certify_id)

    @staticmethod
    def init_smart_verify_auto_route(
            request: cloudauth_models.InitSmartVerifyRequest,
    ) -> cloudauth_models.InitSmartVerifyResponse:
        endpoints = [
            'cloudauth.cn-shanghai.aliyuncs.com',
            'cloudauth.cn-beijing.aliyuncs.com'
        ]
        for endpoint in endpoints:
            try:
                response = InitSmartVerify.init_smart_verify(endpoint, request)
                if UtilClient.equal_string('500', response.code):
                    continue
                return response
            except Exception as err:
                # 网络异常,切换到下个区域调用。
                continue
        return None

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

if __name__ == '__main__':
    InitSmartVerify.main(sys.argv[1:])

DescribeSmartVerify接口调用示例

import sys
from typing import List

from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_cloudauth20200618.client import Client as CloudauthClient
from alibabacloud_cloudauth20200618 import models as cloudauth_models
from alibabacloud_tea_util.client import Client as UtilClient
from alibabacloud_tea_util import models as util_models
from alibabacloud_credentials.client import Client as CredClient

class DescribeSmartVerify:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
            endpoint: str,
    ) -> CloudauthClient:
        # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        # 本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见 https://help.aliyun.com/zh/sdk/developer-reference/configure-credentials。
        cred=CredClient()
        config = open_api_models.Config(
            credential=cred,
            # 设置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(
            args: List[str],
    ) -> None:

        request = cloudauth_models.DescribeSmartVerifyRequest(
            # 请输入场景ID。
            scene_id=xxx,
            certify_id='xxx'
        )
        response = DescribeSmartVerify.describe_smart_verify_auto_route(request)
        # 不支持服务自动路由。
        # response = DescribeSmartVerify.describe_smart_verify('cloudauth.cn-shanghai.aliyuncs.com', request)
        if response is not None:
            print(response.request_id)
            print(response.code)
            print(response.message)
        if response.result_object is not None:
            print(response.result_object.passed)
            print(response.result_object.sub_code)
            print(response.result_object.material_info)
            print(response.result_object.passed_score)

    @staticmethod
    def describe_smart_verify_auto_route(
            request: cloudauth_models.DescribeSmartVerifyRequest,
    ) -> cloudauth_models.DescribeSmartVerifyResponse:
        endpoints = [
            'cloudauth.cn-shanghai.aliyuncs.com',
            'cloudauth.cn-beijing.aliyuncs.com'
        ]
        for endpoint in endpoints:
            try:
                response = DescribeSmartVerify.describe_smart_verify(endpoint, request)
                if UtilClient.equal_string('500', response.code):
                    continue
                return response
            except Exception as err:
                # 网络异常,切换到下个区域调用。
                continue
        return None

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

if __name__ == '__main__':
    DescribeSmartVerify.main(sys.argv[1:])