Indicate the desired document, tutorial or example showing how to add a specific authentication token to a specific header in the created Swagger API in Python?
Here is what I tried:
My API works fine with the right curl command:
curl -v -H 'X-CAG-Authorization: AG_CONSUMER_TOKEN access-key=31337-70k3n' \ 'https://api.company.net/api/v1/user/detail?user=1' * Trying 10.10.1.10... * Connected to api.company.net (10.10.1.10) port 443 (
However, when I try to execute the same basic request in my Python client (2.7.12), I get an authorization failure, despite the fact that the token enters it into the header that will be used. More details about the correct way to use the client or how to get more details about the exact request and response will be appreciated.
/Users/me/VEnvs/sku-grade/bin/python /Users/me/prj/code/python_client/api_example.py HEADERS: {'X-CAG-Authorization': 'AG_CONSUMER_TOKEN access-key=31337-70k3n', 'User-Agent': 'Swagger-Codegen/1.0.0/python'} Exception when calling SupplierApi->get_api_v1_user_details: (401) Reason: Unauthorized HTTP response headers: HTTPHeaderDict({'Date': 'Thu, 22 Dec 2016 21:09:30 GMT', 'Content-Length': '636', 'Content-Type': 'application/json; charset=UTF-8', 'Connection': 'keep-alive', 'Server': 'nginx'}) HTTP response body: { "code" : "PRECONDITION_FAILED", "type" : "UnauthorizedApiDeniedException", "message" : "Target API(/api/v1/user/details) is not available, you have to get a grant in advance.", "messages" : {…
Here's the spagger api spec: swagger.yaml
--- swagger: "2.0" info: description: "API" version: "TEMPORARY" title: "User Details" termsOfService: "http://wiki.company.net/tos" contact: name: "…" license: name: "…" host: "api.company.net" basePath: "/api/v1" tags: - name: "supplier" description: "Supplier" schemes: - "https" produces: - "application/json" paths: /user/details: get: tags: - "supplier" summary: "userDetails" operationId: "getApiV1UserDetails" consumes: - "application/json" produces: - "application/json;charset=utf-8" parameters: - name: "user" in: "query" description: "user id" required: true type: "integer" format: "Long" responses: 200: description: "OK" schema: $ref: "#/definitions/SupplierResponseOfUserDetailsDto" 401: description: "Unauthorized" 403: description: "Forbidden" 404: description: "Not Found" definitions: SupplierResponseOfUserDetailsDto: type: "object" properties: body: $ref: "#/definitions/UserDetailsDto" message: type: "string" successful: type: "boolean" UserDetailsDto: type: "object" properties: name: type: "string"
Swagger-codegen was launched from http://editor.swagger.io/ , and I followed the api example trying to add an extra header: api_example.py
from __future__ import print_function import time import swagger_client from swagger_client import ApiClient from swagger_client import Configuration from swagger_client.rest import ApiException from pprint import pprint
By print(self.api_client.default_headers) in supplier_api.py , I saw that the header was indeed set.
{'X-CAG-Authorization': 'AG_CONSUMER_TOKEN access-key=31337-70k3n', 'User-Agent': 'Swagger-Codegen/1.0.0/python'}
So, what should I change in my example to get it to pass the header and get permission just like a simple curl call does?
Update I also tried to identify it:
security: - api_key: [] securityDefinitions: api_key: type: "apiKey" name: "X-CAG-Authorization" in: "header"
and then install only the key:
swagger_client.configuration.api_key['X-CAG-Authorization'] = \ 'access-key=31337-70k3n' swagger_client.configuration.api_key_prefix['X-CAG-Authorization'] = \ 'AG_CONSUMER_TOKEN'
But this has not changed otherwise than the heading disappears from the default headings that I printed.