你能帮我解决这个SUDS/SOAP问题吗?
我正在尝试使用 SUDS 访问这个 API https://www.clarityaccounting.com/api-docs/。这里是应该可以工作的代码:
from suds.client import Client
client = Client('https://www.clarityaccounting.com/api/v1?wsdl')
token = client.service.doLogin('demo', 'demo', 'www.kashoo.com', 'en_US', 300000)
但是我遇到了这个错误:
WebFault: Server raised fault: 'No such operation: (HTTP GET PATH_INFO: /api/v1)'
他们的支持人员说,请求应该是这样的:
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:api="http://api.service.books/">
<SOAP-ENV:Body>
<api:doLogin>
<username>demo</username>
<password>demo</password>
<siteName>www.kashoo.com</siteName>
<locale>en_US</locale>
<duration>300000</duration>
</api:doLogin>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
但是 SUDS 的请求看起来是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:ns0="http://api.service.books/"
xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns1:Body>
<ns0:doLogin>
<username>demo</username>
<password>demo</password>
<siteName>www.kashoo.com</siteName>
<locale>en_US</locale>
<duration>300000</duration>
</ns0:doLogin>
</ns1:Body>
</SOAP-ENV:Envelope>
我对 SOAP 和 SUDS 还是个新手,但我听说 SUDS 是使用 SOAP 的最佳库,来源于这里:Python 有哪些 SOAP 客户端库,它们的文档在哪里?
所以我想知道的就是,哪些关键部分不同导致请求失败,以及我该如何配置 SUDS 以发送正确格式的请求?
3 个回答
这应该不是和通过HTTPS连接服务有关的问题。我在用suds做类似的事情。我尝试了几种方法来处理你的WSDL文件(我自己也不是专家),结果遇到了同样的错误。不过,作为练习,你可以用suds的factory方法,比如:
login = client.factory.create('doLogin')
login.username = 'username'
etc...
在这里,传给create函数的任何东西都是WSDL文件中定义的某种类型。如果你在命令行中创建了那个类型,可以运行'print login'来查看它的其他属性。
希望这至少能告诉你问题不在于HTTPS。另外,我注意到WSDL文件中没有设置soapAction头,不太确定suds或者服务是如何处理没有这个的请求的。
这里讲的是使用suds-jurko这个库,地址是https://pypi.python.org/pypi/suds-jurko,它是suds的一个维护版本。你可以通过一个叫做__inject的选项,直接传入你想发送的原始xml数据。
from suds.client import Client
username, password, sitename, locale, duration = 'demo', 'demo', 'www.kashoo.com', 'en_US', 300000
raw_xml = """<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:api="http://api.service.books/">
<SOAP-ENV:Body>
<api:doLogin>
<username>{0}</username>
<password>{1}</password>
<siteName>{2}</siteName>
<locale>{3}</locale>
<duration>{4}</duration>
</api:doLogin>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>""".format(username, password, sitename, locale, duration)
client = Client(url, location)
result = client.service.doLogin(__inject={'msg':raw_xml})
我觉得有必要记录一下如何查看suds生成的原始soap信息。
在创建客户端时使用nosend标志。需要注意的是,当这个标志设置为True时,suds只会生成soap,但不会发送它。
client =Client(url, nosend=True)
res = client.service.example()
print res.envelope
#这行代码会打印出原始soap信息使用日志记录。这里我们只记录suds.transport.http,所以它只会输出发送和接收的内容。
import logging
import sys
handler = logging.StreamHandler(sys.stderr)
logger = logging.getLogger('suds.transport.http')
logger.setLevel(logging.DEBUG), handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
使用MessagePlugin
from suds.plugin import MessagePlugin
class MyPlugin(MessagePlugin):
def marshalled(self, context):
#import pdb; pdb.set_trace()
print context.envelope.str()
client = Client(url, plugins=[MyPlugin()])
MessagePlugin不仅可以让你查看生成的soap,还可以在发送之前对它进行修改,具体可以参考这个链接:https://jortel.fedorapeople.org/suds/doc/suds.plugin.MessagePlugin-class.html
乍一看,你遇到的问题似乎和SSL有关。你正在访问一个https的网址,而suds.client的传输处理器默认是用http的。
问题
如果你查看WSDL的底部,会发现它指定的默认位置是http://www.clarityaccounting.com/api/v1
,这是一个http的网址,但WSDL是SSL的。
<wsdl:service name="v1">
<wsdl:port binding="tns:v1SoapBinding" name="BooksApiV1Port">
<soap:address location="http://www.clarityaccounting.com/api/v1"/>
</wsdl:port>
</wsdl:service>
如果你对那个网址进行http GET请求,就会得到你收到的错误信息:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>No such operation: (HTTP GET PATH_INFO: /api/v1)</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
解决方案
要解决这个问题,你需要在调用Client
构造函数时覆盖默认位置,让它使用https:
>>> url
'https://www.clarityaccounting.com/api/v1?wsdl'
>>> client = Client(url, location='https://www.clarityaccounting.com/api/v1')
>>> token = client.service.doLogin('demo', 'demo', 'www.kashoo.com', 'en_US', 300000)
>>> token
(authToken){
authenticationCode = "ObaicdMJZY6UM8xZ2wzGjicT0jQ="
expiryDate = 2010-03-05 12:31:41.000698
locale = "en_US"
myUserId = 4163
site = "www.kashoo.com"
}
胜利!
未来调试时的一个小技巧:开启完整的日志调试。SUDS使用的是标准的logging
库,这样你可以获得很多控制权。所以我把它调到DEBUG
:
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)
logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)
这帮助我缩小了问题范围,因为它清楚地显示出正在通过http发送:
DEBUG:suds.transport.http:sending:
URL:http://www.clarityaccounting.com/api/v1
(xml output omitted)
然后响应也说了这一点:
DEBUG:suds.client:http failed: