python连接阿里云接口进行实名认证

阿里云上有很多身份证实名认证接口,有身份证二要素验证也有身份证图片验证,根据自己的需求选择。

几乎每个套餐都会有20次的免费试用,并且会给出不同语言的请求示例。

我使用的是下面这个链接,同样用pycharm的朋友建议选择请求方式为GET的接口,用到request库非常方便,如果选择请求方式为POST的接口就要用到urllib,可能会因为pycharm版本的原因还要再去解决安装urllib的问题。

https://market.aliyun.com/products/57000002/cmapi022049.html?spm=5176.2020520132.101.3.5bed7218ZrR4Gm#sku=yuncode1604900000

请求信息主要是这几行代码,把身份证号和姓名当作变量,重新拼接url,和我一样的小白,你也可以像我一样把url打印出来,方便函数的编写过程中找出格式错误,由于querys部分是字符串格式,我把它拆成了四个字符串,并且用str()强制转换一下,虽然笨一点但是很实用。

以下是我的代码和认证结果,亲测能用。

import requests
def ID_request(ID,name):
    host = 'https://idcert.market.alicloudapi.com'
    path = '/idcard'
    method = 'GET'
    appcode = '872b*******************'#开通服务后 买家中心-管理控制台-查看AppCode
    idcard='idCard='
    n='&name='
    id=ID
    name=name
    bodys = {}
    url = host + path + '?' + str(idcard)+str(id)+str(n)+str(name)
    header = {"Authorization":'APPCODE ' + appcode}
    try:
        res = requests.get(url,headers=header)
    except :
        print("URL错误")
        exit()
    httpStatusCode = res.status_code
    if(httpStatusCode == 200):
        print('\n'+ res.text)
        print(httpStatusCode)
        print(str(url))   #检查一下url格式对不对
    else:
        httpReason = res.headers['X-Ca-Error-Message']
        if(httpStatusCode == 400 and httpReason == 'Invalid Param Location'):
            print("参数错误")
        elif(httpStatusCode == 400 and httpReason == 'Invalid AppCode'):
            print("AppCode错误")
        elif(httpStatusCode == 400 and httpReason == 'Invalid Url'):
            print("请求的 Method、Path 或者环境错误")
        elif(httpStatusCode == 403 and httpReason == 'Unauthorized'):
            print("服务未被授权(或URL和Path不正确)")
        elif(httpStatusCode == 403 and httpReason == 'Quota Exhausted'):
            print("套餐包次数用完")
        elif(httpStatusCode == 403 and httpReason == 'Api Market Subscription quota exhausted'):
            print("套餐包次数用完,请续购套餐")
        elif(httpStatusCode == 500 ):
            print("API网关错误")
        else:
            print("参数名错误 或 其他错误")
            print(httpStatusCode)
            print(httpReason)

#测试一下函数
id='2*****************'  #身份证号
name='XX'                #姓名 
result=ID_request(ID=id,name=name)
print(result)

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值