繁体   English   中英

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

[英]Change xmlns:wsse namespace in SOAP request with Python Zeep

使用 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 中,就可以了。

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

我花了 2 天,但找到了解决方法。

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()),
        ])
    )
])

)

然后将这样的命名空间 URL 信息添加到所有属性中,而不仅仅是组

  header = xsd.Element(
'{http://schemas.xmlsoap.org/ws/2002/07/secext}Security',
xsd.ComplexType([
    xsd.Element(
        '{http://schemas.xmlsoap.org/ws/2002/07/secext}UsernameToken',
        xsd.ComplexType([
            xsd.Element('{http://schemas.xmlsoap.org/ws/2002/07/secext}Username', xsd.String()),
            xsd.Element('{http://schemas.xmlsoap.org/ws/2002/07/secext}Password', xsd.String()),
        ])
    )
])

)

还要确保像这样将名称空间放在客户端上

client.set_ns_prefix('wsse', "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM