栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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)

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号