.NET SDK调用示例

本文介绍人脸静默活体检测API的.NET SDK调用示例。

SDK使用说明

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

调试LivenessFaceVerify接口

前提条件

  • 需要使用的.NET Framework为4.5.0及以上版本或者.NET Standard为2.0及以上版本。

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

安装SDK

关于.NET SDK源代码,请参见.NET SDK包

SDK有以下两种安装方式,请选择合适的方式安装:

  • 通过.NET命令行安装。

    dotnet add package AlibabaCloud.SDK.Cloudauth20190307 --version 2.0.3
    dotnet add package Aliyun.Credentials
  • 使用包管理安装。

    Install-Package AlibabaCloud.SDK.Cloudauth20190307 -Version 2.0.3
    Install-Package Aliyun.Credentials
说明

Credentials工具配置方法,请参见身份验证配置

调用示例

LivenessFaceVerify接口调用示例

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

using System;

using System.Collections.Generic;
using Tea;

namespace LivenessFaceVerify
{
    public class Program
    {

        public static AlibabaCloud.SDK.Cloudauth20190307.Client CreateClient(string endpoint)
        {
           AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
               Credential=new Aliyun.Credentials.Client(null)
            };
  					config.Endpoint = endpoint;
            return new AlibabaCloud.SDK.Cloudauth20190307.Client(config);
        }

        public static void Main(string[] args)
        {
            AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyRequest request = new AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyRequest
            {
                // 固定值。
                ProductCode = "LR_FR_MIN",
                // 请输入场景ID。
                SceneId = xxx,
                OuterOrderNo = "商户请求的唯一标识",
                Model = "FRONT_CAMERA_LIVENESS",
                // Ip = "用户IP地址",
                // UserId = "用户ID",
                // Mobile = "130xxxxxxxx",
                // DeviceToken = "通过设备指纹获取的deviceToken",

                // 请在以下四种方式中选择其一。
                // 1.照片Base64编码。
                // 2.照片OSS Bucket名和文件名。
                // 3.照片OSS的URL地址。

                // 方式一:照片Base64。
                //FaceContrastPicture = "人脸照片Base64编码",
                // 方式二:照片OSS Bucket名和文件名。
                // OssBucketName = "cn-shanghai-aliyun-cloudauth-xxxxx",
                // OssObjectName = "verify/xxxxx/xxxxxx.jpeg",
                // 方式三:照片OSS的URL地址,公网可访问。
                FaceContrastPictureUrl = "https://cn-shanghai-aliyun-cloudauth-xxxxxx.oss-cn-shanghai.aliyuncs.com/verify/xxxxx/xxxxx.jpeg",
            };
            AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyResponse response = LivenessFaceVerifyAutoRoute(request);
            // 不支持服务自动路由。
            // AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyResponse response = LivenessFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
            Console.WriteLine(response.Body.RequestId);
            Console.WriteLine(response.Body.Code);
            Console.WriteLine(response.Body.Message);
            Console.WriteLine(response.Body.ResultObject.Passed);
            Console.WriteLine(response.Body.ResultObject.SubCode);
            Console.WriteLine(response.Body.ResultObject.MaterialInfo);
            Console.WriteLine(response.Body.ResultObject.CertifyId);
        }


        public static AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyResponse LivenessFaceVerifyAutoRoute(AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyRequest request)
        {
            List<string> endpoints = new List<string>
            {
                "cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com"
            };
            AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyResponse lastResponse = null;

            foreach (var endpoint in endpoints)
            {
                try
                {
                    AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyResponse response = LivenessFaceVerify(endpoint, request);
                    lastResponse = response;
                    if (response is not null)
                    {
                        if (AlibabaCloud.TeaUtil.Common.EqualNumber(500, (int)(response.StatusCode)))
                        {
                            continue;
                        }

                        if (response.Body is not null)
                        {
                            if (AlibabaCloud.TeaUtil.Common.EqualString("500", response.Body.Code))
                            {
                                continue;
                            }
                        }
                    }
                    return response;
                }
                catch (TeaException err)
                {
                    Console.WriteLine(err);
                    // 网络异常,切换到下个区域调用。
                    if (AlibabaCloud.TeaUtil.Common.EqualString("ServiceUnavailable", err.Code))
                    {
                        continue;
                    }
                }
                catch (Exception _err)
                {
                    Console.WriteLine(_err);
                    continue;
                }
            }
            return lastResponse;
        }

        public static AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyResponse LivenessFaceVerify(string endpoint, AlibabaCloud.SDK.Cloudauth20190307.Models.LivenessFaceVerifyRequest request)
        {
            AlibabaCloud.SDK.Cloudauth20190307.Client client = CreateClient(endpoint);
            // 创建RuntimeObject实例并设置运行参数。
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            runtime.ReadTimeout = 10000;
            runtime.ConnectTimeout = 10000;
            return client.LivenessFaceVerifyWithOptions(request, runtime);
        }
    }
}