简体   繁体   中英

Bearer Token authorization header in SOAP client with Zeep and Python

I am new to SOAP requests and programming in general. I would like to access a WSDL that requires a Bearer Token Authorization to use one of their services.

Info about the service I want to access after calling 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:

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[...]

I have tried to pass the header as dict in _soapheaders but not working.

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.

This is how they add the token in 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??

I have seen many post with SOAP+Python in SOF but could not solve the problem. Even with Zeep documentation I have not been able to make it work.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM