suds (SOAP) - 服务器错误 - 未指定参数'username

1 投票
1 回答
6054 浏览
提问于 2025-04-18 10:28

我正在尝试使用一个通过SOAP定义的网络服务中的方法。我已经成功连接并获取了关于这个方法的信息,但当我尝试使用这个方法时,出现了一个错误,提示:

服务器返回错误: '未指定的参数 'username''

所以我猜测,当我想把数据发送到服务器时,出于某种原因,它试图在没有用户名的情况下发送数据?我尝试了在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

完整的错误信息和信封看起来是这样的:

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

'username' 服务器返回错误: '未指定的参数 'username''

我还尝试了这里类似问题的解决方案:http://blogs.it.ox.ac.uk/inapickle/2011/05/14/exchange-web-services-suds-and-python/,但没有帮助。更新 - 我还尝试了一些suds的分支,但问题依旧(具体来说是suds-jurkosuds-ews)。

我该如何强制suds使用这个方法,并指定认证信息(如果之后可能缺少其他细节的话)?

更新2 当我像这样更改认证实现时(实际上与我上面发布的方法非常相似):

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 个回答

1

我终于找到了解决办法。只有在我手动指定用户名和密码的头信息时,才能进行身份验证,像这样:

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

这个想法来自JK1在如何在suds 0.3.6中添加http头信息?中的回答。

撰写回答