How to get Swagger to send api_key in Header and in request URL?

依然范特西╮ 提交于 2020-01-30 08:16:16

问题


I am able to either get the api key to be represented as a header or as a tag on the end of the url, but I am needing it to be both. Is there anyway for this to be possible? The picture is linked here


回答1:


Define both the header and the query parameter in the securityDefinitions section (in OpenAPI 2.0) or the components/securitySchemes section (in OpenAPI 3.0) of your API definition:

# swagger: '2.0'

securityDefinitions:
  apiKeyHeader:
    type: apiKey
    in: header
    name: X-EGEN-AccessTokenID
  apiKeyQueryParam:
    type: apiKey
    in: query
    name: api_key   # replace with your query param name

Then, if you need both the header and query param be passed in the same request:

security:
  - apiKeyHeader: []
    apiKeyQueryParam: []

Or if either the header or query param should be used, but not both:

security:
  - apiKeyHeader: []
  - apiKeyQueryParam: []

More info here: http://swagger.io/docs/specification/authentication/api-keys/

In Swagger UI, when you click "Authorize", you will be enter the values for both the header and the query parameter.




回答2:


window.swaggerUi.api.clientAuthorizations.add(swashbuckleConfig.apiKeyName, new SwaggerClient.ApiKeyAuthorization(swashbuckleConfig.apiKeyName, key, "header"));
      window.swaggerUi.api.clientAuthorizations.add(swashbuckleConfig.apiKeyName + " query", new SwaggerClient.ApiKeyAuthorization(swashbuckleConfig.apiKeyName, key, "query"));


来源:https://stackoverflow.com/questions/44265683/how-to-get-swagger-to-send-api-key-in-header-and-in-request-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!