【发布时间】:2015-09-01 17:00:47
【问题描述】:
我需要使用如下消息调用 SOAP 服务:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="https://secure.xpslogic.com/service/wijnen/sub">
<soapenv:Header>
<sub:auth>
<token>?</token>
<!--Optional:-->
<user_id>?</user_id>
<!--Optional:-->
<user_token>?</user_token>
</sub:auth>
</soapenv:Header>
<soapenv:Body>
<sub:customer_logos_pull>
<!--Optional:-->
<language>?</language>
<!--Optional:-->
<limit>?</limit>
<!--Optional:-->
<options_utc>?</options_utc>
</sub:customer_logos_pull>
</soapenv:Body>
</soapenv:Envelope>
我有一些 php 示例代码,它将标题设置如下(并且效果很好):
auth = 数组(); $auth['token'] = 'xxx'; 如果 ($auth) { // 添加认证头 $this->clients[$module]->__setSoapHeaders( 新的肥皂头( $命名空间, '认证', $认证 ) ); }
我现在使用Python suds lib 构造(空)正文和标题,如下所示:
from suds.client import Client
from suds import WebFault
client = Client(url='https://example.com/sub.wsdl')
auth = client.factory.create('auth')
auth.token = 'xxx'
client.set_options(soapheaders=auth)
customerLogosPull = client.factory.create('customer_logos_pull')
result = client.service.customer_logos_pull(customerLogosPull)
但这给了我一个not well-formed (invalid token) 消息。打开日志记录时,我发现这是消息:
DEBUG:suds.mx.core:processing:
(Content){
tag = "auth"
value =
(auth){
token = "xxx"
user_id = None
user_token = None
}
type = <Element:0x10ff8c950 name="auth">
<Complex:0x10ff8cbd0>
<Sequence:0x10ff8cc50>
<Element:0x10ff8cd10 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
<Element:0x10ff8cd50 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
<Element:0x10ff8cd90 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
</Sequence>
</Complex>
</Element>
}
对我来说它看起来不错,但它也给出了not well-formed (invalid token)。看到suds docs has 3 examples 关于如何传入soap headers,我也尝试了其他两个:
>>> token = client.factory.create('auth.token')
>>> token.set(TOKEN)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: token instance has no attribute 'set'
和
>>> client.set_options(soapheaders={'auth': {'token': 'xxx'}})
>>> customerLogosPull = client.factory.create('customer_logos_pull')
>>> result = client.service.customer_logos_pull(customerLogosPull)
在日志中给出了这个内容,仍然是not well-formed (invalid token):
(Content){
tag = "auth"
value =
{
token = "xxx"
}
type = <Element:0x106049290 name="auth">
<Complex:0x106049510>
<Sequence:0x106049590>
<Element:0x106049650 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
<Element:0x106049690 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
<Element:0x1060496d0 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
</Sequence>
</Complex>
</Element>
}
有人知道如何使用 Python 在标头中正确设置令牌吗?欢迎所有提示!
【问题讨论】:
-
您是如何获取日志来为您格式化日志的?
-
如果需要设置多个soap header,见my answer here
标签: python soap header wsdl suds