suds(SOAP) - 服务器引发的错误 - 未指定参数'用户名'

时间:2014-06-20 06:35:18

标签: python web-services soap suds

我尝试使用soap定义的webservice中的方法。我能够连接并获取有关方法的信息,但是当我尝试使用该方法时,我得到错误说

Server raised fault: 'Unspecified parameter 'username''

所以当我想将数据发送到服务器时,我猜是因为某种原因它试图在没有用户名的情况下发送数据? 我尝试连接(authenticate)到web服务suds文档中描述的各种方法:https://fedorahosted.org/suds/wiki/Documentation

任何一种方法都会产生上述错误。

例如我写的是这样的:

# -*- encoding: utf-8 -*-
import logging
from suds.client import Client
from suds import WebFault
#from suds.transport.https import WindowsHttpAuthenticated
from suds.transport.http import HttpAuthenticated
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

url = 'some_url?wsdl'
t = HttpAuthenticated()
client = Client(url, transport=t, username='username', password='password')
#print client
card = client.factory.create('Type1')
cs = card.subtype
cs.param1 = 25
cs.param2 = 26
cs.param3 = '1234567891'



try:
    card_created = client.service.method1(card)
except WebFault, e:
    print e

Envelope的整体错误如下所示:

DEBUG:suds.client:http failed:
<?xml version='1.0' encoding='UTF-8'?><S:Envelope
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><S:Fault     
xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"><faultcode>S:Server</faultcode>  
<faultstring>Unspecified parameter 'username'</faultstring><detail><ns2:Exception  
xmlns:ns2="http://some_other_url/"><message>Unspecified parameter

&#39;用户名&#39;     服务器引发的错误:&#39;未指定参数&#39;用户名&#39;

我也尝试过类似问题的解决方案: http://blogs.it.ox.ac.uk/inapickle/2011/05/14/exchange-web-services-suds-and-python/ 但它没有任何帮助。 更新 - 我也试过了一些suds forks,但问题仍然存在(具体而言 - suds-jurkosuds-ews)。

如何通过指定身份验证来强制suds使用方法(以及之后可能会丢失的其他详细信息)?

UPDATE2 当我改变这样的身份验证实现时(实际上与我上面发布的方法非常相似):

from suds.transport.http import HttpAuthenticated
t = HttpAuthenticated(username='username', password='password')
client = Client(url, transport=t)

然后当我尝试使用方法(我上面写的相同)时,我得到了这个错误:

    <i>Hypertext Transfer Protocol -- HTTP/1.1</i>:</H3>
    </FONT><FONT FACE="Helvetica" SIZE="3"><H4>10.4.2 401 Unauthorized</H4>
    </FONT><P><FONT FACE="Courier New">The request requires user authentication. The
 response MUST include a WWW-Authenticate header field (section 14.46) containing a 
challenge applicable to the requested resource. The client MAY repeat the request with a
 suitable Authorization header field (section 14.8). If the request already included 
Authorization credentials, then the 401 response indicates that authorization has been 
refused for those credentials. If the 401 response contains the same challenge as the 
prior response, and the user agent has already attempted authentication at least once,
 then the user SHOULD be presented the entity that was given in the response, since that
 entity MAY include relevant diagnostic information. HTTP access authentication is 
explained in section 11.</FONT></P>

所以我认为以前的方法服务器没有看到用户名,但现在由于某种原因它只是拒绝授权?可能是这样的吗?

1 个答案:

答案 0 :(得分:1)

终于找到了解决方案。我只能在“手动”指定用户名/密码标题时进行身份验证:

client = Client(url, headers = {'username': 'username', 'password': 'password'})

想法来自How to add http headers in suds 0.3.6?

中的JK1回答