如何获取Timewriter SOAP API的securityToken用于后续请求
问题
我需要通过SOAP API从Timewriter应用获取数据,用Python编写了初始调用代码,传入api_key和user_key后得到200响应,但返回的XML中securityToken为空,无法用于后续API请求。
我的Python代码如下:
import requests url = "https://procloud.timewriter.nl/wsdl/timewriterapi.wsdl" payload = """<?xml version="1.0" encoding="utf-8"?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <S:Body> <ns2:Login xmlns:ns2="api.timewriter.nl"> <api_key>...key...</api_key> <user_key>...key...</user_key> <softwareDesc></softwareDesc> </ns2:Login> </S:Body> </S:Envelope>""" headers = { 'Content-Type': 'text/xml; charset=utf-8' } response = requests.request("POST", url, headers=headers, data=payload) print(response)
实际返回的XML内容:
<?xml version="1.0" encoding="utf-8"?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <S:Body> <ns2:LoginResponse xmlns:ns2="api.timewriter.nl"> <result> <error></error> <errorMessage/> <securityToken></securityToken> </result> </ns2:LoginResponse> </S:Body> </S:Envelope>
根据官方文档,Login接口的响应应包含有效的securityToken,请问如何获取有效的securityToken?
解决建议
- 校验密钥有效性:确认
api_key和user_key是否完全正确,是否拥有调用Login接口的权限。可以尝试在Timewriter管理后台重新生成密钥后再次请求。 - 填充softwareDesc字段:虽然该字段看似可选,但部分SOAP服务会要求其非空,尝试填入应用标识(如"Python Timewriter Client")后重新发送请求。
- 确认请求端点地址:检查请求URL是否为实际的SOAP调用端点,而非WSDL文件地址。部分服务的WSDL地址和调用端点不同,需查阅官方文档确认正确的POST地址。
- 检查XML格式与命名空间:确保请求XML的命名空间
api.timewriter.nl与官方要求一致,同时避免XML结构中存在可能导致解析异常的多余缩进或格式错误。 - 查看完整响应文本:不要仅打印
response对象,改为打印response.text,检查是否存在隐藏的错误提示信息,部分服务会在空的error字段之外返回更详细的调试内容。
内容的提问来源于stack exchange,提问作者ALdo
相关产品推荐
相关产品推荐

