简体   繁体   English

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

[英]Unable to pass authentication paramethers inside Python SOAP call

I have an WSDL file 我有一个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>

My code in Python is following: 我在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'))

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. 我同时尝试了zeep和suds库,但无法传递此消息。 When I do this call inside of SOPA UI, I get no errors. 当我在SOPA UI内执行此调用时,没有任何错误。 Any ideas what I am doing wrong? 有什么想法我做错了吗?

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): 以下是我解决该问题所遵循的步骤(我使用“ zeep”第3方模块来解决此问题):

  1. Run the following command to understand my WSDL : 运行以下命令以了解我的WSDL

    python -mzeep **wsdl_url**

    (in your case wsdl_url would be http://bmc2012.comtrade.co.yu:8080/arsys/WSDL/public/BMC2012/TestWebService ) (在您的情况下, wsdl_urlhttp://bmc2012.comtrade.co.yu:8080/arsys/WSDL/public/BMC2012/TestWebService

  2. We can search for string "Service:". 我们可以搜索字符串“ Service:”。 Below that we can see our operation name (I guess your operation is "Pokupi"). 在其下面,我们可以看到我们的操作名称(我想您的操作是“ 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" 它清楚地表明,我必须使用kwargs“ _soapheaders”传递一个字符串参数和一个auth参数

  4. With that I came to know that I have to pass my authentication element as _soapheaders argument to MyOperation function. 这样,我就知道必须将身份验证元素作为_soapheaders参数传递给MyOperation函数。

  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. 尽管这是一个较晚的答复,但我认为这对像我这样受苦的人没有什么帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM