gpt4 book ai didi

python - 你能帮我解决这个 SUDS/SOAP 问题吗?

转载 作者:太空狗 更新时间:2023-10-29 17:10:08 25 4
gpt4 key购买 nike

所以我正在尝试访问此 api https://www.clarityaccounting.com/api-docs/使用 SOAP 水。这是应该工作的代码:

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 以发送格式正确的请求?

最佳答案

乍一看,您遇到的问题似乎与 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>

如果您对该 URL 执行 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"
}

胜利!

用于 future 调试目的的专业提示:打开完整的日志记录调试。 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:

关于python - 你能帮我解决这个 SUDS/SOAP 问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2388046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com