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

SDK使用说明

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

详细信息,请参见调试ContrastFaceVerify接口

前提条件

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

引入依赖包

go get github.com/alibabacloud-go/tea-rpc
go get github.com/alibabacloud-go/tea-utils
go get github.com/alibabacloud-go/tea

安装SDK

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

通过Go mod来管理项目依赖:

go get github.com/alibabacloud-go/cloudauth-20190307@vv1.0.5

照片实人认证调用示例

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

package main

import (
  "fmt"
  cloudauth "github.com/alibabacloud-go/cloudauth-20190307/client"
  rpc "github.com/alibabacloud-go/tea-rpc/client"
  util "github.com/alibabacloud-go/tea-utils/service"
  "github.com/alibabacloud-go/tea/tea"
)


func CreateClient (endpoint *string) (_result *cloudauth.Client, _err error) {
  config := &rpc.Config{
    // 您的AccessKey ID。
    AccessKeyId: tea.String("您的AccessKey ID"),
    // 您的AccessKey Secret。
    AccessKeySecret: tea.String("您的AccessKey Secret"),
    // 设置HTTP代理。
    // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"),
    // 设置HTTPS代理。
    // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"),
    Endpoint: endpoint,
  }
  _result = &cloudauth.Client{}
  _result, _err = cloudauth.NewClient(config)
  return _result, _err
}

func _main () (_err error) {
  request := &cloudauth.ContrastFaceVerifyRequest{
    // 固定值。
    ProductCode: tea.String("ID_MIN"),
    // 请输入场景ID。
    SceneId: tea.Int64(xxx),
    OuterOrderNo: tea.String("商户请求的唯一标识"),
    Model: tea.String("NO_LIVENESS"),
    // 固定值IDENTITY_CARD。
    CertType: tea.String("IDENTITY_CARD"),
    CertName: tea.String("xxx"),
    CertNo: tea.String("xxx"),
    // Ip: tea.String("用户IP地址"),
    // UserId: tea.String("用户ID"),
    // Mobile: tea.String("130xxxxxxxx"),
    // DeviceToken: tea.String("通过设备指纹获取的deviceToken"),

    // 请在以下四种方式中选择其一。
    // 1.照片Base64编码。
    // 2.照片OSS Bucket名和文件名。
    // 3.照片OSS的URL地址。
    // 4.已经实人认证通过的CertifyId。
    // 方式一:照片Base64。
    // FaceContrastPicture: tea.String("人脸照片Base64编码"),
    // 方式二:照片OSS Bucket名和文件名。
    // OssBucketName: tea.String("cn-shanghai-aliyun-cloudauth-xxxxx"),
    // OssObjectName: tea.String("verify/xxxxx/xxxxxx.jpeg"),
    // 方式三:照片OSS的URL地址,公网可访问。
    FaceContrastPictureUrl: tea.String("https://cn-shanghai-aliyun-cloudauth-xxxxxx.oss-cn-shanghai.aliyuncs.com/verify/xxxxx/xxxxx.jpeg"),
    // 方式四:已经实人认证通过的CertifyId。
    // CertifyId: tea.String("0bfa7c493f850e5178b9f8613634c9xx"),
  }
  response, _err := ContrastFaceVerifyAutoRoute(request)
  // 不支持服务自动路由。
  // response, _err := ContrastFaceVerify(tea.String("cloudauth.cn-shanghai.aliyuncs.com"), request)
  if _err != nil {
    return _err
  }

  fmt.Println(*response.RequestId)
  fmt.Println(*response.Code)
  fmt.Println(*response.Message)
  fmt.Println(*response.RequestId)
  fmt.Println(*response.ResultObject.Passed)
  fmt.Println(*response.ResultObject.IdentityInfo)
  fmt.Println(*response.ResultObject.SubCode)
  fmt.Println(*response.ResultObject.MaterialInfo)
  return _err
}

func ContrastFaceVerifyAutoRoute (request *cloudauth.ContrastFaceVerifyRequest) (_result *cloudauth.ContrastFaceVerifyResponse, _err error) {
  endpoints := []*string{tea.String("cloudauth.cn-shanghai.aliyuncs.com"), tea.String("cloudauth.cn-beijing.aliyuncs.com")}
  var lastResponse *cloudauth.ContrastFaceVerifyResponse
  for _, endpoint := range endpoints {
    response, _err := ContrastFaceVerify(endpoint, request)
    if _err != nil {
      var err = &tea.SDKError{}
      if _t, ok := _err.(*tea.SDKError); ok {
        err = _t
        // 网络异常,切换到下个区域调用。
        if tea.BoolValue(util.EqualString(tea.String("ServiceUnavailable"), err.Code)) {
          continue
        }
      }
      return _result, _err
    }

    if tea.BoolValue(util.EqualString(tea.String("500"), response.Code)) {
      continue
    }

    _result = response
    return _result, _err

  }
  _result = lastResponse
  return _result, _err
}

func ContrastFaceVerify (endpoint *string, request *cloudauth.ContrastFaceVerifyRequest) (_result *cloudauth.ContrastFaceVerifyResponse, _err error) {
  client, _err := CreateClient(endpoint)
  if _err != nil {
    return _result, _err
  }

  // 创建RuntimeObject实例并设置运行参数。
  runtime := &util.RuntimeOptions{}
  runtime.ReadTimeout = tea.Int(10000)
  runtime.ConnectTimeout = tea.Int(10000)
  _result = &cloudauth.ContrastFaceVerifyResponse{}
  _body, _err := client.ContrastFaceVerify(request, runtime)
  if _err != nil {
    return _result, _err
  }
  _result = _body
  return _result, _err
}


func main() {
  err := _main()
  if err != nil {
    panic(err)
  }
}

视频实人认证调用示例

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

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

package main

import (
  "fmt"
  cloudauth "github.com/alibabacloud-go/cloudauth-20190307/client"
  rpc "github.com/alibabacloud-go/tea-rpc/client"
  util "github.com/alibabacloud-go/tea-utils/service"
  "github.com/alibabacloud-go/tea/tea"
  "os"
)


func CreateClient (endpoint *string) (_result *cloudauth.Client, _err error) {
  config := &rpc.Config{
    // 您的AccessKey ID。
    AccessKeyId: tea.String("您的AccessKey ID"),
    // 您的AccessKey Secret。
    AccessKeySecret: tea.String("您的AccessKey Secret"),
    // 设置HTTP代理。
    // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"),
    // 设置HTTPS代理。
    // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"),
    Endpoint: endpoint,
  }
  _result = &cloudauth.Client{}
  _result, _err = cloudauth.NewClient(config)
  return _result, _err
}

func _main () (_err error) {
  f, err := os.Open("/xxx/xxx.mp4")
  if err != nil {
    return err
  }
  request := &cloudauth.ContrastFaceVerifyAdvanceRequest{
    // 固定值。
    ProductCode: tea.String("ID_MIN_VIDEO"),
    // 请输入场景ID。
    SceneId: tea.Int64(xxx),
    OuterOrderNo: tea.String("商户请求的唯一标识"),
    Model: tea.String("FRONT_CAMERA_LIVENESS"),
    // 固定值IDENTITY_CARD。
    CertType: tea.String("IDENTITY_CARD"),
    CertName: tea.String("xxx"),
    CertNo: tea.String("xxx"),
    // Ip: tea.String("用户IP地址"),
    // UserId: tea.String("用户ID"),
    // Mobile: tea.String("130xxxxxxxx"),
    // DeviceToken: tea.String("通过设备指纹获取的deviceToken"),
    FaceContrastFileObject: f,
  }
  response, _err := ContrastFaceVerifyAutoRoute(request)
  // 不支持服务自动路由。
  // response, _err := ContrastFaceVerify(tea.String("cloudauth.cn-shanghai.aliyuncs.com"), request)
  f.Close()
  if _err != nil {
    return _err
  }

  fmt.Println(*response.RequestId)
  fmt.Println(*response.Code)
  fmt.Println(*response.Message)
  fmt.Println(*response.ResultObject.Passed)
  fmt.Println(*response.ResultObject.IdentityInfo)
  fmt.Println(*response.ResultObject.SubCode)
  fmt.Println(*response.ResultObject.MaterialInfo)
  return _err
}

func ContrastFaceVerifyAutoRoute (request *cloudauth.ContrastFaceVerifyAdvanceRequest) (_result *cloudauth.ContrastFaceVerifyResponse, _err error) {
  endpoints := []*string{tea.String("cloudauth.cn-shanghai.aliyuncs.com"), tea.String("cloudauth.cn-beijing.aliyuncs.com")}
  var lastResponse *cloudauth.ContrastFaceVerifyResponse
  for _, endpoint := range endpoints {
    response, _err := ContrastFaceVerify(endpoint, request)
    if _err != nil {
      var err = &tea.SDKError{}
      if _t, ok := _err.(*tea.SDKError); ok {
        err = _t
        // 网络异常,切换到下个区域调用。
        if tea.BoolValue(util.EqualString(tea.String("ServiceUnavailable"), err.Code)) {
          continue
        }
      }
      return _result, _err
    }

    if tea.BoolValue(util.EqualString(tea.String("500"), response.Code)) {
      continue
    }

    _result = response
    return _result, _err

  }
  _result = lastResponse
  return _result, _err
}

func ContrastFaceVerify (endpoint *string, request *cloudauth.ContrastFaceVerifyAdvanceRequest) (_result *cloudauth.ContrastFaceVerifyResponse, _err error) {
  client, _err := CreateClient(endpoint)
  if _err != nil {
    return _result, _err
  }

  // 创建RuntimeObject实例并设置运行参数。
  runtime := &util.RuntimeOptions{}
  runtime.ReadTimeout = tea.Int(10000)
  runtime.ConnectTimeout = tea.Int(10000)
  _result = &cloudauth.ContrastFaceVerifyResponse{}
  _body, _err := client.ContrastFaceVerifyAdvance(request, runtime)
  if _err != nil {
    return _result, _err
  }
  _result = _body
  return _result, _err
}


func main() {
  err := _main()
  if err != nil {
    panic(err)
  }
}

使用OSS对象存储的方式

package main

import (
  "fmt"
  cloudauth "github.com/alibabacloud-go/cloudauth-20190307/client"
  rpc "github.com/alibabacloud-go/tea-rpc/client"
  util "github.com/alibabacloud-go/tea-utils/service"
  "github.com/alibabacloud-go/tea/tea"
)


func CreateClient (endpoint *string) (_result *cloudauth.Client, _err error) {
  config := &rpc.Config{
    // 您的AccessKey ID。
    AccessKeyId: tea.String("您的AccessKey ID"),
    // 您的AccessKey Secret。
    AccessKeySecret: tea.String("您的AccessKey Secret"),
    // 设置HTTP代理。
    // HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"),
    // 设置HTTPS代理。
    // HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"),
    Endpoint: endpoint,
  }
  _result = &cloudauth.Client{}
  _result, _err = cloudauth.NewClient(config)
  return _result, _err
}

func _main () (_err error) {
  request := &cloudauth.ContrastFaceVerifyRequest{
    // 固定值。
    ProductCode: tea.String("ID_MIN_VIDEO"),
    // 请输入场景ID。
    SceneId: tea.Int64(xxx),
    OuterOrderNo: tea.String("商户请求的唯一标识"),
    Model: tea.String("FRONT_CAMERA_LIVENESS"),
    // 固定值IDENTITY_CARD。
    CertType: tea.String("IDENTITY_CARD"),
    CertName: tea.String("xxx"),
    CertNo: tea.String("xxx"),
    // Ip: tea.String("用户IP地址"),
    // UserId: tea.String("用户ID"),
    // Mobile: tea.String("130xxxxxxxx"),
    // DeviceToken: tea.String("通过设备指纹获取的deviceToken"),
    OssBucketName: tea.String("cn-shanghai-aliyun-cloudauth-xxxxx"),
    OssObjectName: tea.String("verify/xxxxx/xxxxxx.mp4"),
  }
  // response, _err := ContrastFaceVerifyAutoRoute(request)
  // 不支持服务自动路由。
  response, _err := ContrastFaceVerify(tea.String("cloudauth.cn-shanghai.aliyuncs.com"), request)
  if _err != nil {
    return _err
  }

  fmt.Println(*response.RequestId)
  fmt.Println(*response.Code)
  fmt.Println(*response.Message)
  fmt.Println(*response.ResultObject.Passed)
  fmt.Println(*response.ResultObject.IdentityInfo)
  fmt.Println(*response.ResultObject.SubCode)
  fmt.Println(*response.ResultObject.MaterialInfo)
  fmt.Println(*response.ResultObject.CertifyId)
  return _err
}

func ContrastFaceVerifyAutoRoute (request *cloudauth.ContrastFaceVerifyRequest) (_result *cloudauth.ContrastFaceVerifyResponse, _err error) {
  endpoints := []*string{tea.String("cloudauth.cn-shanghai.aliyuncs.com"), tea.String("cloudauth.cn-beijing.aliyuncs.com")}
  var lastResponse *cloudauth.ContrastFaceVerifyResponse
  for _, endpoint := range endpoints {
    response, _err := ContrastFaceVerify(endpoint, request)
    if _err != nil {
      var err = &tea.SDKError{}
      if _t, ok := _err.(*tea.SDKError); ok {
        err = _t
        // 网络异常,切换到下个区域调用。
        if tea.BoolValue(util.EqualString(tea.String("ServiceUnavailable"), err.Code)) {
          continue
        }
      }
      return _result, _err
    }

    if tea.BoolValue(util.EqualString(tea.String("500"), response.Code)) {
      continue
    }

    _result = response
    return _result, _err

  }
  _result = lastResponse
  return _result, _err
}

func ContrastFaceVerify (endpoint *string, request *cloudauth.ContrastFaceVerifyRequest) (_result *cloudauth.ContrastFaceVerifyResponse, _err error) {
  client, _err := CreateClient(endpoint)
  if _err != nil {
    return _result, _err
  }

  // 创建RuntimeObject实例并设置运行参数。
  runtime := &util.RuntimeOptions{}
  runtime.ReadTimeout = tea.Int(10000)
  runtime.ConnectTimeout = tea.Int(10000)
  _result = &cloudauth.ContrastFaceVerifyResponse{}
  _body, _err := client.ContrastFaceVerify(request, runtime)
  if _err != nil {
    return _result, _err
  }
  _result = _body
  return _result, _err
}


func main() {
  err := _main()
  if err != nil {
    panic(err)
  }
}