我刚刚开始使用DHL-SOAP之一,并使用zeep对API运行请求。API期望和元素身份验证如下:
...
<soapenv:Header>
<cis:Authentification>
<cis:user>USER</cis:user>
<cis:signature>PASSWORD</cis:signature>
</cis:Authentification>
</soapenv:Header>
...我试图像zeep文档中所描述的那样,将身份化作为_soapheaders的一部分来传递,对dict符号和xsd.Element标记似乎都起作用。
from zeep import Client
from zeep import xsd
client = Client('<URL_TO_WSDL>')
auth_header = {'user': 'user', 'signature': 'signature'}
# dict approach
client.service.DHL_SERVICE(_soapheaders={'Authentification': auth_header})
# xsd approach
header = xsd.Element('Authentification',
xsd.ComplexType([
xsd.Element('user', xsd.String()),
xsd.Element('signature', xsd.String())
])
)
header_values = header(user='user', signature='signature')
client.service.DHL_SERVICE(_soapheaders=[header_values])我在DHL文档中找不到有用的信息,在zeep文档中也没有。
提前谢谢你!
问候
发布于 2020-02-18 23:36:43
以防有人遇到同样的问题。结果表明,客户端需要使用HTTPBasicAuth在网关上进行身份验证。此外,必须使用带有会话的传输来创建客户端,其中包含网关身份验证标头。使得xsd头方法工作的是添加的{http://test.python-zeep.org}。此设置使与API的通信工作平稳。
from requests import Session
from requests.auth import HTTPBasicAuth
from zeep import Client
from zeep.transports import Transport
session = Session()
# Authenticate with gateway
session.auth = HTTPBasicAuth(user, password)
client = Client(WSDL_PATH, transport=Transport(session=session))
# Build Authentification header for API-Endpoint using zeep xsd
header = xsd.Element(
'{http://test.python-zeep.org}Authentification',
xsd.ComplexType([
xsd.Element(
'{http://test.python-zeep.org}user',
xsd.String()),
xsd.Element(
'{http://test.python-zeep.org}signature',
xsd.String()),
])
)
header_value = header(user=USER, signature=PASSWORD)
client.service.DHL_SERVICE(_soapheaders=[header_value])https://stackoverflow.com/questions/60212850
复制相似问题