how to configure api key (codegen / python)

25 views
Skip to first unread message

John Hogenmiller

unread,
Mar 23, 2017, 12:30:20 AM3/23/17
to Swagger
I can't find a clear example of what to put into the configuration dict for api_key.

I essentially need the client to send a header "Authorization: <token>"

Here is my current code with workaround and some attempts at doing the configuration method.

from __future__ import print_function
import swagger_client
from swagger_client.configuration import Configuration

# this is the secret stuff to get a token
data = dict(username='myusername', password='mysecret')
url = 'http://localhost:8000/api/v1'

# configure it
c = Configuration()
c.host = url
c.debug = False

# authenticate and get token
nonauth_client = swagger_client.DefaultApi()
login = nonauth_client.authenticate(data)
apikey = login['access_token']

# configure to use token - WHAT TO PUT HERE?
# c.api_key = apikey
# c.api_key = {"api_key": apikey}

# workaround: create an auth_client that uses the token
auth_client = swagger_client.ApiClient(vm_url, 'Authorization', apikey)
app = swagger_client.DefaultApi(auth_client)

# add a serviceline
# app.serviceline_add({'name': 'foobar'})

# print the servicelines
print(app.servicelines_get())
Reply all
Reply to author
Forward
0 new messages