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

时间:2017-03-31 14:18:33

标签: python python-2.7 soap wsdl soap-client

我有一个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>

我在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'))

我不断收到数据作为回应,而是不断收到错误消息:    必须在控制记录中提供用户名

我尝试使用zeep和suds库,但我无法传递此消息。当我在SOPA UI内部进行此调用时,我没有错误。 我有什么想法吗?

1 个答案:

答案 0 :(得分:5)

我遇到过类似的问题。以下是我为解决问题而采取的步骤(我使用了#34; zeep&#34;第三方模块来解决这个问题):

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

    python -mzeep **wsdl_url**

    (在您的情况下 wsdl_url 将是http://bmc2012.comtrade.co.yu:8080/arsys/WSDL/public/BMC2012/TestWebService

  2. 我们可以搜索字符串&#34;服务:&#34;。在下面我们可以看到我们的操作名称(我猜你的操作是&#34; Pokupi&#34;)。

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

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

    清楚地传达了这一点,我必须使用kwargs传递一个字符串参数和一个auth param&#34; _soapheaders&#34;

  4. 随后我知道我必须将我的身份验证元素作为_soapheaders参数传递给MyOperation函数。

  5. 创建的Auth元素:

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

  6. 将auth传递给我的操作:

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

  7. 我得到了预期的结果。虽然这是一个迟到的回复,我认为这会帮助很少像我这样受苦的人。