我试图使用Python中的SUDS访问这个API https://www.kashoo.com/api-docs。在
我使用的Python代码如下:
>>> from suds.client import Client
>>> client = Client('https://www.kashoo.com/api/v1?wsdl')
>>> token = client.service.doLogin('username', 'password', 'www.kashoo.com', 'en_US', 3000000)
authToken的创建没有问题:
^{pr2}$问题是当我试图对令牌进行编码并生成编码的身份验证字符串时
>>>encodedtoken = client.service.encodeAuthToken(token)
Traceback (most recent call last):
File "<console>", line 0, in <module>
File "C:\Python27\lib\suds\client.py", line 542, in __call__
return client.invoke(args, kwargs)
File "C:\Python27\lib\suds\client.py", line 602, in invoke
result = self.send(soapenv)
File "C:\Python27\lib\suds\client.py", line 649, in send
result = self.failed(binding, e)
File "C:\Python27\lib\suds\client.py", line 702, in failed
r, p = binding.get_fault(reply)
File "C:\Python27\lib\suds\bindings\binding.py", line 265, in get_fault
raise WebFault(p, faultroot)
WebFault: Server raised fault: 'Token authentication code is incorrect'
问题似乎出在令牌中的时间格式。调用doLogin函数收到的信封如下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:doLoginResponse xmlns:ns2="https://www.kashoo.com/api/">
<token authenticationCode="0WM1DgdaAFrJ7Yz4Up2UWnbVsZk=" expiryDate="2011-07-11T03:33:24.046-07:00" locale="en_US" myUserId="531772668" site="www.kashoo.com"/>
</ns2:doLoginResponse>
</soap:Body>
</soap:Envelope>
如果我使用soapUI这样的工具在kashooapi中传递该令牌,它可以正常工作。但是,当我使用SUDS从Python调用encodeAuthToken函数时,会生成以下SOAP信封:
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://www.kashoo.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:encodeAuthToken>
<token authenticationCode="0WM1DgdaAFrJ7Yz4Up2UWnbVsZk=" expiryDate="2011-07-11T11:33:24.000046+01:00" locale="en_US" myUserId="531772668" site="www.kashoo.com"/>
</ns1:encodeAuthToken>
</ns0:Body>
</SOAP-ENV:Envelope>
请注意expiryDate属性的时间格式是如何更改的:
<token authenticationCode="0WM1DgdaAFrJ7Yz4Up2UWnbVsZk=" expiryDate="2011-07-11T03:33:24.046-07:00" locale="en_US" myUserId="531772668" site="www.kashoo.com"/>
<token authenticationCode="0WM1DgdaAFrJ7Yz4Up2UWnbVsZk=" expiryDate="2011-07-11T11:33:24.000046+01:00" locale="en_US" myUserId="531772668" site="www.kashoo.com"/>
在接收到的令牌中,时间是用毫秒指定的,而在传递的令牌中,时间是以微秒为单位指定的。在
问题解决了。在
在SUDS库中,我修改了肥皂水.萨克斯.日期218号线模块更换
return dt.time(hour, minute, second, ms)
与
return dt.time(hour, minute, second, 1000 * ms)
现在,当我获得一个令牌时,time属性被正确地解释和传递
>>> print token1
(authToken){
_locale = "en_US"
_authenticationCode = "JQyior8Qprg3+3wuZo8B5JnN3c8="
_myUserId = 531772668
_site = "www.kashoo.com"
_expiryDate = 2011-07-11 18:36:38.136000
}
>>> token2 = client.service.encodeAuthToken(token1)
>>> print token2
6454e3af-b09d-4484-90b3-ea2632ab9fe4
我是个编程新手,这个解决方案远不是很好,但似乎很管用。在
感谢@dobesv的反馈和指导。在
如果您可以生成一个日志,记录作为HTTP请求和响应“通过网络”传递的内容,那么它可能有助于诊断问题。你做的似乎是对的。在
还有其他的电话吗?怎么样service.getmybusiness服务(代币)?在
您还可以考虑使用restapi而不是SOAP,看看这是否有帮助。在
相关问题 更多 >
编程相关推荐