python3 使用阿里云接口进行身份证实名认证 如今随着互联网产业的多元化发展,尤其是互联网金融,O2O,共享经济等新兴商业形式的兴起,企业对实名认证业务的数据形式和数据质量有了更高的需求。如今也衍生出身份证实名认证业务,通过接口将身份证号码、姓名上传至阿里云,再与全国公民身份信息系统进行匹配,判断信息的一致性。

在使用接口服务的方面我推荐使用技术实力强大的阿里云;

首先点击:(阿里云API接口)获取相应的订单后在控制台中可以得到您的appcode;

python代码如下:

import urllib, urllib2, sys
import ssl


host = 'https://idenauthen.market.alicloudapi.com'
path = '/idenAuthentication'
method = 'POST'
appcode = '你自己的AppCode'
querys = ''
bodys = {}
url = host + path

bodys['idNo'] = '''340421190210182345'''
bodys['name'] = '''张三'''
post_data = urllib.urlencode(bodys)
request = urllib2.Request(url, post_data)
request.add_header('Authorization', 'APPCODE ' + appcode)
//根据API的要求定义相对应的Content-Type
request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
response = urllib2.urlopen(request, context=ctx)
content = response.read()
if (content):
    print(content)

正常返回示例

{
  "name": "张三",
  "idNo": "340421190710145412",
  "respMessage": "身份证信息匹配",
  "respCode": "0000",
  "province": "安徽省",
  "city": "淮南市",
  "county": "凤台县",
  "birthday": "19071014",
  "sex": "M",
  "age": "111"
}

失败返回示例

{
  "name": "李四",
  "idNo": "340421190710145412",
  "respMessage": "身份证信息不匹配",
  "respCode": "0008",
  "province": "安徽省",
  "city": "淮南市",
  "county": "凤台县",
  "birthday": "19071014",
  "sex": "M",
  "age": "111"
}