Python-zeep: Irregular Authentication with Headers

Created on 3 Jun 2016  路  7Comments  路  Source: mvantellingen/python-zeep

Hi,

I'm trying to authenticate with Trafficvance API.
for example: http://apitest.trafficvance.com/?v3=campaigns.wsdl

Their API requires and AuthenticateRequest with each request.
Quoting their documentation:

Authentication credentials are required to be specified within the SOAP XML request header. You need to invoke AuthenticateRequest
operation and provide the API key as its only parameter, please see available examples for more details

I can't find any way to use this API with Zeep library.
No matter what I try I get zeep.exceptions.Fault: Access denied, authentication failed

Is it even possible?

docs requirefeedback

Most helpful comment

With an API Key this example code made it work for us.

-- UPDATE: 2018-01-21

Fixed spelling mistakes pointed out below

from zeep import Client, xsd

API_KEY_TEST = 'YOUR_OWN_API_KEY'
WSDL_TEST = 'https://apitest.trafficvance.com/?v3=system.wsdl'

client = Client(WSDL)
header = xsd.Element(
    '{WSDL_TEST}AuthenticateRequest',
    xsd.ComplexType([
        xsd.Element(
            '{WSDL_TEST}apiKey', xsd.String()
        )
    ])
)
header_value = header(apiKey=API_KEY_TEST)

res = client.service.getServerTime(_soapheaders=[header_value])

All 7 comments

You can send the headers when you make the operation call by setting _soapheader along with it, eg:

client.service.getCampaignIds(filterNames=['filtered name'], _soapheader={'apiKey': 'myApiKey'})

Ok thanks! authentications seems to be working now!
I think It should be added to documentation..

But now I get
Requested operation failed [Description: Invalid operation requested] [Service: Campaigns Service v3] [Operation: addCampaignRequest]
no matter what I try.. dont know if its related to how zeep works or the API itself

Hm, I'm afraid I need more information to do something with this. Not familiair with the API.
What you can do is use soap-ui to do the request and if that works we can compare the XML generated by soap-ui with the xml generated by zeep

Looks like you're trying to call addCampaignRequest directly, you want to call something like:

client.service.addCampaign(campaign={'name': 'foo', ...})

You'll need to make sure you fill out whatever data is required by the API, which from that wsdl it looks like all of the fields of a CampaignAdd complex type need to be between 1-250 chars.

60 seems to be a continuation of the issue with the addCampaign() call. So closing this. (if i'm wrong please let me know)

With an API Key this example code made it work for us.

-- UPDATE: 2018-01-21

Fixed spelling mistakes pointed out below

from zeep import Client, xsd

API_KEY_TEST = 'YOUR_OWN_API_KEY'
WSDL_TEST = 'https://apitest.trafficvance.com/?v3=system.wsdl'

client = Client(WSDL)
header = xsd.Element(
    '{WSDL_TEST}AuthenticateRequest',
    xsd.ComplexType([
        xsd.Element(
            '{WSDL_TEST}apiKey', xsd.String()
        )
    ])
)
header_value = header(apiKey=API_KEY_TEST)

res = client.service.getServerTime(_soapheaders=[header_value])

@MichaelSvendsen I was working on something similar and this really helped. Thanks.

By the way, in your code you've declared WSDL_TEST but misspelt it further down as WDSL_TEST

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexdemari picture alexdemari  路  4Comments

orzilca picture orzilca  路  6Comments

grguthrie picture grguthrie  路  4Comments

pope1ni picture pope1ni  路  4Comments

ryan-mccaffrey picture ryan-mccaffrey  路  5Comments