使用Python Zeep在SOAP请求中更改xmlns:wsse命名空间

问题描述 投票:0回答:1

使用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
1个回答
1
投票

通过切换到使用不同的SOAP库解决。

© www.soinside.com 2019 - 2024. All rights reserved.