无法在 Python SOAP 调用中传递身份验证参数 [英] Unable to pass authentication paramethers inside Python SOAP call

查看:78
本文介绍了无法在 Python SOAP 调用中传递身份验证参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WSDL 文件

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>

我的 Python 代码如下:

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

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

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?

推荐答案

我也遇到过类似的问题.以下是我解决该问题的步骤(我使用zeep"第 3 方模块来解决此问题):

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. 运行以下命令以了解我的WSDL:

python -mzeep **wsdl_url**

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

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

我们可以搜索字符串Service:".下面我们可以看到我们的操作名称(我猜你的操作是Pokupi").

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

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

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

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

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

创建的身份验证元素:

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.

这篇关于无法在 Python SOAP 调用中传递身份验证参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆