小编Vin*_*nat的帖子

使用Swagger生成的客户端时标头中的硬编码Api键值

我在C#中编写了许多API,并使用Swashbuckle创建了一个"Swagger"文档网站.

对于Authenticate REST调用,我在标头中使用API​​ Key.

我创建了一个页面,允许下载任何编程语言的特定客户端,如下所示:https://generator.swagger.io

我想让用户使用自己的API密钥生成客户端,这样他就不需要再在代码中手动设置API密钥了.

在我的Swagger JSON中,我有这个安全性定义:

"securityDefinitions": {
    "apiKey": {
        "type": "apiKey",
        "description": "API Key Authentication",
        "name": "X-ApiKey",
        "in": "header"
    }
}
Run Code Online (Sandbox Code Playgroud)

在Swagger Client Generator的页面中,我发现这个模型允许设置客户端选项,但我无法找到客户端代码中API密钥如何(以及是否)可以硬编码(或任何其他类型的授权).

GeneratorInput {
    spec (object, optional),
    options (object, optional),
    swaggerUrl (string, optional),
    authorizationValue (AuthorizationValue, optional),
    securityDefinition (SecuritySchemeDefinition, optional)
}
AuthorizationValue {
    value (string, optional),
    type (string, optional),
    keyName (string, optional)
}
SecuritySchemeDefinition {
    description (string, optional),
    type (string, optional)
}
Run Code Online (Sandbox Code Playgroud)

我想我必须设置AuthorizationValue对象,但没有关于它的文档(或者我找不到它).

能够让生成的客户端lib为所有请求添加任意HTTP头就足够了.

在这种情况下,我们可以添加:

X-ApiKey:{whatever the key is}
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

非常感谢!

api-key swagger swashbuckle swagger-codegen

15
推荐指数
1
解决办法
1025
查看次数

标签 统计

api-key ×1

swagger ×1

swagger-codegen ×1

swashbuckle ×1