问题
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):
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)
We can search for string "Service:". Below that we can see our operation name (I guess your operation is "Pokupi").
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"
With that I came to know that I have to pass my authentication element as _soapheaders argument to MyOperation function.
Created Auth Element:
auth_ele = client.get_element('ns0:AuthenticationInfo') auth = auth_ele(userName='me', password='mypwd')
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