我是 SOAP 请求和一般编程的新手。我想访问需要持有者令牌授权才能使用其服务之一的 WSDL。
调用后我想访问的服务的信息 pyhton -mzeep *WSDL_url*:
getInfo(param1: xsd:string, param2: xsd:anySimpleType, param3: xsd:anySimpleType) -> out: ns0:ResponseCurve[]
Run Code Online (Sandbox Code Playgroud)
首先,我收到令牌:
import zeep
user = 'my_user'
userpass = 'my_pass'
token = client.service.getAuthToken(user,userpass)
Run Code Online (Sandbox Code Playgroud)
然后我想请求需要三个参数的服务 getInfo:
my_info = client.service.getInfo('param1', 'param2', 'param3')
Run Code Online (Sandbox Code Playgroud)
我从提供商那里知道,每次我想访问此服务时都需要令牌,并且在文档中,关于身份验证的标头说明了以下内容:
授权:承载 eyJhbGciOiJIUzI1N[...]
我试图将标题作为 dict 传入 _soapheaders但没有工作。
我可以使用强制请求访问服务:
def get_response_from_provider(token, param1, param2, param3):
url = "WSDL url"
headers = {'Authorization': 'Bearer ' + token,
'content-type': 'text/xml'}
body = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsl="uri">
<soapenv:Header/>
<soapenv:Body>
<wsl:getInfo>
<param1>""" + param1 + """</param1>
<param2>""" + param2 + """ </param2>
<param3>""" + param3 + """ </param3>
</wsl:getInfo>
</soapenv:Body>
</soapenv:Envelope>"""
response = requests.post(url, data=body, headers=headers)
print("Info recieved...")
return response
Run Code Online (Sandbox Code Playgroud)
但是我想通过 SOAP 客户端访问服务。
这是他们在 PHP 中添加令牌的方式:
$soap->soapClient->_stream_context = stream_context_create([
'http' => [
'header' => sprintf('Authorization: Bearer %s', $authTokenResponse->token)
]
]);
Run Code Online (Sandbox Code Playgroud)
关于如何将带有令牌的标头添加到 Python 中的客户端请求的任何想法?
我在 SOF 中看到了很多关于 SOAP+Python 的帖子,但都无法解决问题。即使使用 Zeep 文档,我也无法使其工作。
谢谢
小智 5
我想做类似的事情,结果它在文档中,但它有点隐藏,你可以在这里找到它:
https://python-zeep.readthedocs.io/en/master/settings.html#context-manager
简而言之,您可以执行以下操作:
import zeep
settings = zeep.Settings(extra_http_headers={'Authorization': 'Bearer ' + token})
client = zeep.Client(wsdl=url, settings=settings)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2688 次 |
| 最近记录: |