6

I've generated a Python client library for this API by using the online Swagger Codegen at https://generator.swagger.io/. The API uses Bearer authentication:

openapi: 3.0.0
...

paths:
  /sandbox/register:
    post:
      ...
      security:
        - sso_auth: []
      ...

components:
  securitySchemes:
    sso_auth:
      type: http
      scheme: bearer

However, the Configuration class in generated Python client has no access_token field.

How to fill the Bearer access token when using the generated client library?


The codegen endpoint POST /gen/clients/{language} has the authorizationValue and securityDefinition parameters - do I need to configure these parameters somehow?

"authorizationValue": {
  "value": "string",
  "type": "string",
  "keyName": "string"
},
"securityDefinition": {
  "type": "string",
  "description": "string"
}
Helen
  • 87,344
  • 17
  • 243
  • 314
Awethon
  • 144
  • 1
  • 9

3 Answers3

11

First of all, since your API is OpenAPI 3.0 you need to use Swagger Codegen 3.x, i.e. https://generator3.swagger.io or swagger-codegen-cli-3.0.11.jar. The generator at https://generator.swagger.io is for OpenAPI 2.0 (swagger: '2.0').

That said, there's a bug in the Python generator of Swagger Codegen 3.x, it doesn't generate the code for Bearer authentication in OpenAPI 3.0 definitions. Please file a bug report at https://github.com/swagger-api/swagger-codegen-generators/issues

The authorizationValue and securityDefinition parameters of /gen/clients are not related to security definitions in OpenAPI files.


As a workaround, edit your OpenAPI YAML file and replace this part

  securitySchemes:
    sso_auth:
      type: http
      scheme: bearer

with:

  securitySchemes:
    sso_auth:
      type: apiKey
      in: header
      name: Authorization

Then generate a new Python client from the modified API definition.

Now, once you have installed the client package as explained in README.md, you should be able to set the token as follows:

import swagger_client
...

# Configure API key authorization: sso_auth
configuration = swagger_client.Configuration()
configuration.api_key['Authorization'] = 'YOUR_BEARER_TOKEN'
configuration.api_key_prefix['Authorization'] = 'Bearer'

# create an instance of the API class
api_instance = swagger_client.MarketApi(swagger_client.ApiClient(configuration))
...
Helen
  • 87,344
  • 17
  • 243
  • 314
1

In my case I could simply set access_token of Configuration object like this:

configuration = Configuration()
configuration.verify_ssl = ...
configuration.host = ...
configuration.access_token = token

Information taken from here, schema generated by fastapi.

enter image description here

Tobias Ernst
  • 4,214
  • 1
  • 32
  • 30
0

Another way of doing that is to use the bearer security scheme, as follows:

openapi-definition.json

{
  "openapi": "3.0.1",
  ...
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    }
  },
  "security": [
    {"bearerAuth":  []}
  ]

Python Client

import swagger_client
...

# Configure API authorization
configuration = swagger_client.Configuration()
configuration.access_token = 'YOUR_BEARER_TOKEN'

# create an instance of the API class
api_instance = swagger_client.AnApi(swagger_client.ApiClient(configuration))
...

Tested with OpenApi 3.0.1

Documentation: https://swagger.io/docs/specification/authentication/bearer-authentication/