【发布时间】:2019-04-10 16:11:32
【问题描述】:
使用 Zeep (Python3.7) 向 SOAP API 发送数据时,生成的 wsse:Security 标头为 http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd
这样的结果就是错误:
zeep.exceptions.Fault: SOAP Security Header UsernameToken is required for operation 'ProcessMessage'
如果我随后获取原始请求 XML 并将其发送到 API(通过 SOAPUI),我会遇到同样的问题。但是,如果我将此值更改为示例中与http://schemas.xmlsoap.org/ws/2002/07/secext 的 WSDL 一起发送的值,则请求将成功完成,并且我会从 API 获得成功响应。
我尝试了很多东西,包括在 Security 元素标头中明确定义命名空间:
header = xsd.Element(
'{http://schemas.xmlsoap.org/ws/2002/07/secext}Security',
xsd.ComplexType([
xsd.Element(
'UsernameToken',
xsd.ComplexType([
xsd.Element('Username', xsd.String()),
xsd.Element('Password', xsd.String()),
])
)
])
)
但是,这似乎并不能解决问题。
我也试过了:
client.set_default_soapheaders([header_value])
再一次,没有快乐。
有没有办法在 Zeep 中做到这一点(我对不同的 SOAP 包持开放态度,尽管 Zeep 似乎是最积极维护的)?还是我的请求格式中完全遗漏了可能导致此问题的某些内容?
代码如下。提前谢谢!
header = xsd.Element(
'Security',
xsd.ComplexType([
xsd.Element(
'UsernameToken',
xsd.ComplexType([
xsd.Element('Username', xsd.String()),
xsd.Element('Password', xsd.String()),
])
)
])
)
header_value = header(UsernameToken={'Username': user, 'Password': password})
client.service.ProcessMessage(_soapheaders=[header_value], Payload=dataobj)
在生成的 XML 方面,上面的示例给出了以下内容:
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
<soap-env:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap-env:Header>
<soap-env:Body>
### REQUEST BODY
</soap-env:Body>
</soap-env:Envelope>
这不起作用
但是,只需将原始 XML 中的 wsse:Security xmlns:wsse 值更改为 http://schemas.xmlsoap.org/ws/2002/07/secext 并将其粘贴到 SOAPUI 中即可。
【问题讨论】:
标签: python python-3.x soap xml-namespaces zeep