文档

App认证接入教程

更新时间:

人脸核身提供服务端SDK帮助您在App中实现刷脸认证功能。本文将结合示例代码进行详细的说明介绍。

说明

阿里云视觉智能开放平台各类目视觉AI能力API接入、接口使用或问题咨询等,请通过钉钉群(23109592)加入阿里云视觉智能开放平台咨询群联系我们。

成为开发者

请您使用阿里云账号登录阿里云视觉智能开放平台。如还未持有阿里云账户,请先注册阿里云账号

开通服务

  1. 登录阿里云视觉智能开放平台
  2. 在顶部菜单栏的能力广场中单击人脸人体
  3. 人脸人体页面中单击立即开通
    说明 对于公测期免费能力,在页面中显示免费开通;对于商业化付费能力,在页面中显示立即开通,开通后不调用不计费,调用后开始收费。
  4. 确认开通地域并勾选服务协议后,单击立即开通

    页面提示服务开通成功。

    说明 开通了人脸人体服务后,该服务下的所有API均可以直接调用,不需要再单独开通。

RAM授权

您还需要为您的账户进行RAM授权,以访问相应的阿里云资源。授权操作详情请参见使用RAM Policy控制访问权限

添加依赖包

请根据您的代码选择添加不同的依赖包。

  • Java

    (推荐)在您的Java项目中添加人脸人体服务的POM依赖。

    <!-- https://mvnrepository.com/artifact/com.aliyun/facebody20191230 -->
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>facebody20191230</artifactId>
        <version>${aliyun.facebody.version}</version>
    </dependency>
                                

    您也可以下载人脸人体服服务的SDK包直接引用。

    AI类目

    SDK包名称

    SDK链接

    Github链接

    人脸人体

    facebody20191230

    Aliyun Java SDK Facebody

    facebody-20191230

  • Python

    打开命令提示符窗口,依次执行如下命令安装Python SDK。

    pip install alibabacloud_facebody20191230

配置环境变量

配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET

重要
  • 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维,具体操作,请参见创建RAM用户

  • 请不要将AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

  • Linux和macOS系统配置方法

    1. 在IntelliJ IDEA中打开终端Terminal。

    2. 执行以下命令,配置环境变量。

      <access_key_id>需替换为您RAM用户的AccessKey ID,<access_key_secret>替换为您RAM用户的AccessKey Secret。如果后续需要进行更多权限相关的配置,具体操作请参见使用RAM Policy控制访问权限

      export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
      export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows系统配置方法

    新建环境变量文件,添加环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET,并写入已准备好的AccessKey ID和AccessKey Secret。然后重启Windows系统。本操作以Windows 10为例进行说明。

    1. 打开文件资源管理器,在此电脑上右键单击属性。

    2. 在右侧导航栏,单击高级系统配置

    3. 系统属性对话框的高级页签下,单击环境变量

    4. 环境变量对话框中,单击新建(W)image.png

    5. 在弹出的新建系统变量对话框中,添加环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET,并写入已准备好的AccessKey ID和AccessKey Secret。

    6. 重启Windows系统,使配置生效。

请求示例

请求示例如下:

  • Java请求示例

    说明 请将如下代码示例中的<accessKeyId><accessSecret>替换为您的真实AK信息。如何获取AK请参见创建AccessKey
    1. 核身认证移动端请求

      package com.lixin.test.viapi.facebody;
      
      import com.aliyun.facebody20191230.models.GenRealPersonVerificationTokenResponse;
      
      public class GenRealPersonVerificationToken {
      
          /**
           * 使用AK&SK初始化账号Client
           * @param accessKeyId
           * @param accessKeySecret
           * @return Client
           * @throws Exception
           */
          public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
              /*
                初始化配置对象com.aliyun.teaopenapi.models.Config
                Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
               */
              com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                      .setAccessKeyId(accessKeyId)
                      .setAccessKeySecret(accessKeySecret);
              config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
              return new com.aliyun.facebody20191230.Client(config);
          }
      
          public static void main(String[] args) throws Exception {
              // 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。
              // 如果您使用的是RAM用户的AccessKey,还需要为RAM用户授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html。
              // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
              com.aliyun.facebody20191230.Client client = GenRealPersonVerificationToken.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
              com.aliyun.facebody20191230.models.GenRealPersonVerificationTokenRequest genRealPersonVerificationTokenRequest = new com.aliyun.facebody20191230.models.GenRealPersonVerificationTokenRequest()
                      .setCertificateName("真实姓名")
                      .setCertificateNumber("身份证号")
                      .setMetaInfo("metainfo环境参数"); //通过端侧SDK获取
              com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
              try {
                  GenRealPersonVerificationTokenResponse resp = client.genRealPersonVerificationTokenWithOptions(genRealPersonVerificationTokenRequest, runtime);
                  // 输出VerificationToken
                  System.out.println(resp.getBody().getData().getVerificationToken());
              } catch (com.aliyun.tea.TeaException teaException) {
                  // 获取整体报错信息
                  System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
                  // 获取单个字段
                  System.out.println(teaException.getCode());
              }
          }
      }                                
      说明

      调用核身认证移动端请求接口返回的verificationToken有效期时间为30分钟,从verificationToken生成时间开始算起,超过有效期再进行人脸认证会报错,请在有效期时间内完成认证。

    2. 在iOS或Android客户端发起请求,接入数据,请参见iOS客户端接入教程Android客户端接入教程

    3. 核身认证移动端查询

      package com.lixin.test.viapi.facebody;
      
      import com.aliyun.facebody20191230.models.GetRealPersonVerificationResultResponse;
      
      public class GetRealPersonVerificationResult {
      
          /**
           * 使用AK&SK初始化账号Client
           * @param accessKeyId
           * @param accessKeySecret
           * @return Client
           * @throws Exception
           */
          public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
              /*
                初始化配置对象com.aliyun.teaopenapi.models.Config
                Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
               */
              com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                      .setAccessKeyId(accessKeyId)
                      .setAccessKeySecret(accessKeySecret);
              config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
              return new com.aliyun.facebody20191230.Client(config);
          }
      
          public static void main(String[] args) throws Exception {
              // 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。
              // 如果您使用的是RAM用户的AccessKey,还需要为RAM用户授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html。
              // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
              com.aliyun.facebody20191230.Client client = GenRealPersonVerificationToken.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
              com.aliyun.facebody20191230.models.GetRealPersonVerificationResultRequest getRealPersonVerificationResultRequest = new com.aliyun.facebody20191230.models.GetRealPersonVerificationResultRequest()
                      .setVerificationToken("verificationToken"); //第一步拿到的结果
              com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
              try {
                  GetRealPersonVerificationResultResponse resp = client.getRealPersonVerificationResultWithOptions(getRealPersonVerificationResultRequest, runtime);
                  // 输出是否认证通过
                  System.out.println(resp.getBody().getData().getPassed());
              } catch (com.aliyun.tea.TeaException teaException) {
                  // 获取整体报错信息
                  System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
                  // 获取单个字段
                  System.out.println(teaException.getCode());
              }
          }
      }        
  • Python请求示例

    说明 请将如下代码示例中的<accessKeyId><accessSecret>替换为您的真实AK信息。如何获取AK请参见创建AccessKey
    1. 核身认证移动端请求

      # -*- coding: utf-8 -*-
      from alibabacloud_facebody20191230.client import Client
      from alibabacloud_facebody20191230.models import GenRealPersonVerificationTokenRequest
      from alibabacloud_oss_util.models import RuntimeOptions
      from alibabacloud_tea_openapi.models import Config
      
      # 初始化Config
      config = Config(
        # 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。
        # 如果您用的是RAM用户的AccessKey,还需要为RAM用户授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html。
        # 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
        access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
        endpoint='facebody.cn-shanghai.aliyuncs.com',
        # 访问的域名对应的region
        region_id='cn-shanghai'
      )
      
      request = GenRealPersonVerificationTokenRequest(
        certificate_name='真实姓名',
        certificate_number='身份证号',
        meta_info='metainfo环境参数' #通过端侧SDK获取
      )
      # 初始化RuntimeObject
      runtime_option = RuntimeOptions()
      
      try:
        # 初始化Client
        client = Client(config)
      
        response = client.gen_real_person_verification_token_with_options(request, runtime_option)
        # 输出VerificationToken
        print(response.body.data.verification_token)
        # tips: 可通过response.body.__dict__查看属性名称
      except Exception as error:
        # 获取整体报错信息
        print(error)
        # 获取单个字段
        print(error.code)
        # tips: 可通过error.__dict__查看属性名称
    2. 在iOS或Android客户端发起请求,接入数据,请参见iOS客户端接入教程Android客户端接入教程

    3. 核身认证移动端查询

      
      # -*- coding: utf-8 -*-
      from alibabacloud_facebody20191230.client import Client
      from alibabacloud_facebody20191230.models import GetRealPersonVerificationResultRequest
      from alibabacloud_oss_util.models import RuntimeOptions
      from alibabacloud_tea_openapi.models import Config
      
      # 初始化Config
      config = Config(
        # 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。
        # 如果您用的是RAM用户的AccessKey,还需要为RAM用户授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html。
        # 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
        access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
        endpoint='facebody.cn-shanghai.aliyuncs.com',
        # 访问的域名对应的region
        region_id='cn-shanghai'
      )
      
      request = GetRealPersonVerificationResultRequest(
        verification_token='verificationToken' # 第一步拿到的结果
      )
      # 初始化RuntimeObject
      runtime_option = RuntimeOptions()
      
      try:
        # 初始化Client
        client = Client(config)
      
        response = client.get_real_person_verification_result_with_options(request, runtime_option)
        # 输出Passed
        print(response.body.data.passed)
        # tips: 可通过response.body.__dict__查看属性名称
      except Exception as error:
        # 获取整体报错信息
        print(error)
        # 获取单个字段
        print(error.code)
        # tips: 可通过error.__dict__查看属性名称

结果示例

该接口的请求结果示例如下。

{
  "RequestId" : "10FC953D-7C0C-4915-8949-34E3246E5B79",
  "Data" : {
    "Pass" : "true"
  }
}

从返回结果来看,该图片的人脸与人名和身份证号码相匹配,认证通过。具体信息如下:

  • RequestId:请求ID。

  • Pass:验证结果为通过。

  • 本页导读 (1)
文档反馈