无法在Python SOAP调用中传递身份验证参数

1 python soap wsdl soap-client python-2.7

我有一个WSDL文件

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:TestWebService">
   <soapenv:Header>
      <urn:AuthenticationInfo>
         <urn:userName>test</urn:userName>
         <urn:password>test</urn:password>
         <!--Optional:-->
         <urn:authentication></urn:authentication>
         <!--Optional:-->
         <urn:locale></urn:locale>
         <!--Optional:-->
         <urn:timeZone></urn:timeZone>
      </urn:AuthenticationInfo>
   </soapenv:Header>
   <soapenv:Body>
      <urn:Pokupi>
         <urn:Request_ID__c>000000000000141</urn:Request_ID__c>
      </urn:Pokupi>
   </soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)

我在Python中的代码如下:

#import zeep
from suds.client import Client
from suds import WebFault
from suds.sax.element import Element

url = 'http://bmc2012.comtrade.co.yu:8080/arsys/WSDL/public/BMC2012/TestWebService'
user = "test"
password = "test"

ssnp = Element("xsi:AuthenticationInfo").append(Element('xsi:userName').setText(user))
ssnp = Element("xsi:AuthenticationInfo").append(Element('xsi:password').setText(password))
client = Client(url)
client.set_options(soapheaders=ssnp)

record = client.factory.create('Pokupi')
result = client.service.Pokupi(record)
print result

#client = zeep.Client(url)
#print (client.service.Pokupi('000000000000141'))
Run Code Online (Sandbox Code Playgroud)

我没有得到响应的数据,而是不断收到错误消息:必须在控制记录中提供用户名

我同时尝试了zeep和suds库,但无法传递此消息。当我在SOPA UI内执行此调用时,没有任何错误。有什么想法我做错了吗?

Joh*_*wyn 6

我也遇到过类似的问题。以下是我解决该问题所遵循的步骤(我使用“ zeep”第3方模块来解决此问题):

  1. 运行以下命令以了解我的WSDL

    python -mzeep **wsdl_url**

    (在您的情况下,wsdl_urlhttp://bmc2012.comtrade.co.yu:8080/arsys/WSDL/public/BMC2012/TestWebService

  2. 我们可以搜索字符串“ Service:”。在其下面,我们可以看到我们的操作名称(我想您的操作是“ Pokupi”)。

  3. 对于我的操作,我发现以下条目:

    MyOperation(param1 xsd:string, _soapheaders={parameters: ns0:AuthenticationInfo})

    它清楚地表明,我必须使用kwargs“ _soapheaders”传递一个字符串参数和一个auth参数

  4. 这样,我就知道必须将身份验证元素作为_soapheaders参数传递给MyOperation函数。

  5. 创建的身份验证元素:

    auth_ele = client.get_element('ns0:AuthenticationInfo') auth = auth_ele(userName='me', password='mypwd')

  6. 将身份验证传递给我的操作:

    cleint.service.MyOperation('param1_value', _soapheaders=[auth])

我得到了预期的结果。尽管这是一个很晚的答复,但我认为这对像我这样受苦的人没有什么帮助。