简体   繁体   English

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

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

When using Zeep (Python3.7) to send data to a SOAP API, the wsse:Security header generated is http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd使用 Zeep (Python3.7) 向 SOAP API 发送数据时,生成的wsse:Security标头为http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd

The result of this is the error:这样做的结果是错误:

zeep.exceptions.Fault: SOAP Security Header UsernameToken is required for operation 'ProcessMessage'

If I then take the raw request XML and send that to the API (via SOAPUI), I get the same issue.如果我然后获取原始请求 XML 并将其发送到 API(通过 SOAPUI),我会遇到同样的问题。 However, if I change this value to the value that was in the example I've been sent alongside the WSDL of http://schemas.xmlsoap.org/ws/2002/07/secext , the request completes successfully and I get a success response from the API.但是,如果我将此值更改为示例中的值,我已与http://schemas.xmlsoap.org/ws/2002/07/secext的 WSDL 一起发送,请求将成功完成,我得到一个来自 API 的成功响应。

I have tried many things, including explicitely defining the namespace in Security element header:我尝试了很多事情,包括在 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()),
            ])
        )
    ])
)

However, this doesn't seem to fix the issue.但是,这似乎并不能解决问题。

I've also tried:我也试过:

client.set_default_soapheaders([header_value])

Again, no joy.再一次,没有快乐。

Is there a way of doing this within Zeep (I'm open to a different SOAP package, although Zeep seemed the most actively maintained)?在 Zeep 中有没有办法做到这一点(我对不同的 SOAP 包持开放态度,尽管 Zeep 似乎是最积极维护的)? Or am I missing something in my request format entirely that could be causing this issue?或者我是否完全遗漏了可能导致此问题的请求格式?

Code below.代码如下。 Thank you in advance!先感谢您!

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)

In terms of generated XML, the above example gives the following:就生成的 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>

Which does not work哪个不起作用

However, by simply changing the wsse:Security xmlns:wsse value to http://schemas.xmlsoap.org/ws/2002/07/secext in the raw XML and pasting that into SOAPUI, that works.但是,通过简单地将原始 XML 中的wsse:Security xmlns:wsse值更改为http://schemas.xmlsoap.org/ws/2002/07/secext并将其粘贴到 SOAPUI 中,就可以了。

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

Took me 2 days but found fix for this.我花了 2 天,但找到了解决方法。 take

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

) )

and then add the name space URL info like this to all attributes not just the group然后将这样的命名空间 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()),
        ])
    )
])

) )

also make sure you put the name space on the client like this还要确保像这样将名称空间放在客户端上

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