所以我正在尝试使用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库:What SOAP client libraries exist for Python, and where is the documentation for them?
所以我的问题只是关键部分是什么,这些部分是不同的,并且使请求失败,我如何配置SUDS以发送格式正确的请求?
答案 0 :(得分:37)
乍一看,您遇到的问题就是使用SSL。您正在访问https URL,默认情况下,suds.client的传输处理程序会与http进行对话。
问题
如果查看WSDL的底部,它将默认位置指定为http://www.clarityaccounting.com/api/v1
,这是一个http URL,但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:
答案 1 :(得分:2)
使用suds-jurko https://pypi.python.org/pypi/suds-jurko,这是一个保持肥皂的分叉。 您可以传入一个__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})
我觉得我应该记录所有可以检查肥皂泡沫在这里产生的肥皂的方法。
构建客户端时使用nosend标志。请注意,将标志设置为True,suds将生成肥皂但不发送。
client =Client(url, nosend=True)
res = client.service.example()
print res.envelope
#prints the raw 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,还可以在发送之前对其进行修改,请参阅 - &gt; https://jortel.fedorapeople.org/suds/doc/suds.plugin.MessagePlugin-class.html
答案 2 :(得分:1)
这不应该是通过HTTPS连接服务的问题。我正在使用肥皂水来做同样的事情。我已经尝试了一些你的WSDL文件的方法(我自己不是专家)并遇到了同样的错误。你应该用suds练习做什么就是使用 factory 方法,例如
login = client.factory.create('doLogin')
login.username = 'username'
etc...
发送到create函数的任何内容都是WSDL文件中定义的类型之一。如果在shell中创建该类型,则可以运行“print login”以查看其附加属性。
希望这至少告诉你问题出在哪里(使用HTTPS)。另外,我注意到SOAPAction头没有在WSDL文件中设置,不知道suds或服务如何处理没有它的请求。