Unable to pass authentication paramethers inside Python SOAP call

核能气质少年 提交于 2019-12-23 03:48:26

问题


I have an WSDL file

    <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>

My code in Python is following:

#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'))

Instead of getting data in response, I constantly get error message: A user name must be supplied in the control record

I tried both with zeep and suds library, but I cannot pass this message. When I do this call inside of SOPA UI, I get no errors. Any ideas what I am doing wrong?


回答1:


I have faced similar issue. Following are the steps I followed to solve the issue (I have used "zeep" a 3rd party module to solve this):

  1. Run the following command to understand my WSDL:

    python -mzeep **wsdl_url**

    (in your case wsdl_url would be http://bmc2012.comtrade.co.yu:8080/arsys/WSDL/public/BMC2012/TestWebService)

  2. We can search for string "Service:". Below that we can see our operation name (I guess your operation is "Pokupi").

  3. For my operation I found following entry:

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

    which clearly communicates that, I have to pass one string param and an auth param using kwargs "_soapheaders"

  4. With that I came to know that I have to pass my authentication element as _soapheaders argument to MyOperation function.

  5. Created Auth Element:

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

  6. Passed the auth to my Operation:

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

I got the the expected result. Though this a late reply, I thought this would help few people who suffer like me.



来源:https://stackoverflow.com/questions/43142966/unable-to-pass-authentication-paramethers-inside-python-soap-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!