问题:
??RESTful 的传输协议类型为 HTTPS,鉴权方式为 Digest_SHA256(即DIGEST摘要认证) 时,接口调用问题。
参考资料:
普通的RESTful接口调用:
import requests
from requests.auth import HTTPDigestAuth
def change_password(cur_ip):
payload = {
"ManipulateName": "ApiAdmin",
"ManipulatePwd": "HuaWei123",
"ManipulateModule": 3,
"ManipulateInfo": {
"UserName": "ApiAdmin",
"UserPwd": "HuaWei1234"
}
}
username = 'ApiAdmin'
password = 'HuaWei1234'
url = 'https://{}/SDCAPI/V1.0/AuthIaas/Users'.format(cur_ip)
res = requests.put(url=url,
headers=headers,
auth=HTTPDigestAuth(username, password),
data=payload,
timeout=5)
print(res.text)
代码存在的问题:
因为requests的请求默认开启了验证,导致程序不会用摘要认证HTTPDigestAuth方法对接口返回的401进行处理。![在这里插入图片描述](https://img-blog.csdnimg.cn/9be5f412b1ee4f43a50680dbdf77a136.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUXJlZHN1bg==,size_20,color_FFFFFF,t_70,g_se,x_16
解决方法:
??在put方法中增加参数:verify=False
同理:
??如果RESTful 的传输协议类型为 HTTP,鉴权方式为 Digest_SHA256(即DIGEST摘要认证) 时,接口调用的代码:
import requests
from requests.auth import HTTPDigestAuth
def change_password(cur_ip):
payload = {
"ManipulateName": "ApiAdmin",
"ManipulatePwd": "HuaWei123",
"ManipulateModule": 3,
"ManipulateInfo": {
"UserName": "ApiAdmin",
"UserPwd": "HuaWei1234"
}
}
username = 'ApiAdmin'
password = 'HuaWei1234'
url = 'http://{}/SDCAPI/V1.0/AuthIaas/Users'.format(cur_ip)
res = requests.put(url=url,
headers=headers,
auth=HTTPDigestAuth(username, password),
data=payload,
timeout=5)
print(res.text)
??如果RESTful 的传输协议类型为 HTTPS,鉴权方式为 Digest_MD5(即DIGEST摘要认证) 时,接口调用的代码可以是:
import requests
from requests.auth import HTTPDigestAuth
def change_password(cur_ip):
payload = {
"ManipulateName": "ApiAdmin",
"ManipulatePwd": "HuaWei123",
"ManipulateModule": 3,
"ManipulateInfo": {
"UserName": "ApiAdmin",
"UserPwd": "HuaWei1234"
}
}
username = 'ApiAdmin'
password = 'HuaWei1234'
url = 'https://{}/SDCAPI/V1.0/AuthIaas/Users'.format(cur_ip)
res = requests.put(url=url,
headers=headers,
auth=HTTPDigestAuth(username, password),
data=payload,
verify=False,
timeout=5)
print(res.text)
|