OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.56k stars 6.28k forks source link

Question on the bearer aurhorisation / apikeys #7056

Closed heyudude closed 3 years ago

heyudude commented 3 years ago
Description

I am writing a website generator for a game Brawlstars, their API is defined and they use bearer auth with api keys limited to IP addresses. If I make a manual get request the response is OK. Via the generated code and some tests the response is "Missing Authorisation" The api url format is:

curl -X GET --header 'Accept: application/json' --header "authorization: Bearer <API token>" 'https://api.brawlstars.com/v1/players/<Player id>'
openapi-generator version

version 3.0.0 and tried also with 3.0.3 format, latest 5.0.0 beta and the 4.3.1 stable

OpenAPI declaration file content or url

the part which is relevant:

servers:
  - url: 'https://api.brawlstars.com/v1'
security:
  - JWT: []

Wrong:

components:         
  securitySchemes:
    JWT:
      type: apiKey
      description: JSON Web Token used as an API key
      in: header
      name: authorisation

Correct:

components:         
  securitySchemes:

    JWT:
        type: http
        scheme: bearer
        bearerFormat: JWT
Command line used for generation
java -jar openapi-generator-cli.jar generate -g python -i ../brawlstars-swagger/bs_swagger.yaml --config ../brawlstars-swagger/bs_config.yaml 

Config:

generatorName: python
outputDir: .
inputSpec: bs_swagger.yaml
templateDir: ../brawlstars-swagger/python
additionalProperties:
  packageName: pybrawl
Suggest a fix/enhancement

Any help would be appreciated! Thanks for this wonderful toolset

auto-labeler[bot] commented 3 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

heyudude commented 3 years ago

I figured out the above question by hard coding the value for access_token in the configuration.py Now the api call succeeds!

Next question: where to initialize the value correctly from my settings file I use? Because access_token needs to be assigned manually to has it seems

heyudude commented 3 years ago

Found the solution... no replies unfortunately o a valid question: why?

GabeL7r commented 3 years ago

@heyudude can you share the solution? I'm facing the same problem.

heyudude commented 3 years ago

@GabeL7r Sure. Anytime here to help, this piece of yaml generated the correct code in my API module I had to check also for old and floating modules: I cleaned them all before I build a new one with this change. Just to double check. My yaml can be found at https://github.com/heyudude/brawlstars-swagger I figured out the above question by hard coding the value for access_token in the configuration.py Now the api call succeeds! And the API code is called pybrawl. Good luck with the solution and if I need to peek and help, let me know.

security:
  - JWT: []

components:
  securitySchemes:
    JWT:
        type: http
        scheme: bearer
        bearerFormat: JWT