[英]Bearer Token authorization header in SOAP client with Zeep and Python
I am new to SOAP requests and programming in general.一般来说,我是 SOAP 请求和编程的新手。 I would like to access a WSDL that requires a Bearer Token Authorization to use one of their services.
我想访问需要 Bearer Token Authorization 才能使用其中一项服务的 WSDL。
Info about the service I want to access after calling pyhton -mzeep *WSDL_url*
:有关调用
pyhton -mzeep *WSDL_url*
后我想访问的服务的信息:
getInfo(param1: xsd:string, param2: xsd:anySimpleType, param3: xsd:anySimpleType) -> out: ns0:ResponseCurve[]
First I recieve the token with:首先我收到令牌:
import zeep
user = 'my_user'
userpass = 'my_pass'
token = client.service.getAuthToken(user,userpass)
Then I would like to request the service getInfo that requires three parameters:然后我想请求需要三个参数的服务getInfo:
my_info = client.service.getInfo('param1', 'param2', 'param3')
I know by the provider that the Token is needed each time I want to access this service and in documentation the following is stated about headers regarding Authentification:我从提供者那里知道每次我想访问此服务时都需要令牌,并且在文档中对有关身份验证的标头进行了说明:
Authorization: Bearer eyJhbGciOiJIUzI1N[...]授权:承载 eyJhbGciOiJIUzI1N[...]
I have tried to pass the header as dict in _soapheaders
but not working.我试图在
_soapheaders
标头作为字典传递,但没有用。
I can access the service by using a forced requests:我可以使用强制请求访问该服务:
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
However I would like to access the Services through the SOAP client.但是我想通过 SOAP 客户端访问服务。
This is how they add the token in PHP:这是他们在 PHP 中添加令牌的方式:
$soap->soapClient->_stream_context = stream_context_create([
'http' => [
'header' => sprintf('Authorization: Bearer %s', $authTokenResponse->token)
]
]);
Any Idea on how to add the header with the Token to the client request in Python??关于如何将带有令牌的标头添加到 Python 中的客户端请求的任何想法?
I have seen many post with SOAP+Python in SOF but could not solve the problem.我在 SOF 中看到很多关于 SOAP+Python 的帖子,但无法解决问题。 Even with Zeep documentation I have not been able to make it work.
即使有 Zeep 文档,我也无法使其工作。
Thanks谢谢
I was looking to do something similar, it turns out it is on the documentation but it is kind of hidden, you can find it here:我想做类似的事情,结果它在文档中但它有点隐藏,你可以在这里找到它:
https://python-zeep.readthedocs.io/en/master/settings.html#context-manager https://python-zeep.readthedocs.io/en/master/settings.html#context-manager
In short, you can do something like:简而言之,您可以执行以下操作:
import zeep
settings = zeep.Settings(extra_http_headers={'Authorization': 'Bearer ' + token})
client = zeep.Client(wsdl=url, settings=settings)
I'm not sure why the accepted answer didn't work for me, but here is what I ended up doing我不确定为什么接受的答案对我不起作用,但这就是我最终做的
import requests
from zeep import Client, Transport
headers = {
"Authorization": "Bearer " + get_token()
}
session = requests.Session()
session.headers.update(headers)
transport = Transport(session=session)
client = Client(wsdl=url, transport=transport)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.