本文介绍了金融级实人认证纯服务端API的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_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
from alibabacloud_credentials.client import Client as CredClient
class ContrastFaceVerify:
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() -> 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
from alibabacloud_credentials.client import Client as CredClient
class ContrastFaceVerifyVideo:
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() -> 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
from alibabacloud_credentials.client import Client as CredClient
class ContrastFaceVerifyVideoOSS:
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() -> 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()
文档内容是否对您有帮助?