首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于zeep的DHL-SOAP认证

基于zeep的DHL-SOAP认证
EN

Stack Overflow用户
提问于 2020-02-13 16:48:19
回答 1查看 759关注 0票数 3

我刚刚开始使用DHL-SOAP之一,并使用zeep对API运行请求。API期望和元素身份验证如下:

代码语言:javascript
复制
...
<soapenv:Header>
    <cis:Authentification>
        <cis:user>USER</cis:user>
        <cis:signature>PASSWORD</cis:signature>
    </cis:Authentification>
</soapenv:Header>
...

我试图像zeep文档中所描述的那样,将身份化作为_soapheaders的一部分来传递,对dict符号和xsd.Element标记似乎都起作用。

代码语言:javascript
复制
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文档中也没有。

提前谢谢你!

问候

EN

回答 1

Stack Overflow用户

发布于 2020-02-18 23:36:43

以防有人遇到同样的问题。结果表明,客户端需要使用HTTPBasicAuth在网关上进行身份验证。此外,必须使用带有会话的传输来创建客户端,其中包含网关身份验证标头。使得xsd头方法工作的是添加的{http://test.python-zeep.org}。此设置使与API的通信工作平稳。

代码语言:javascript
复制
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])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60212850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档