![](/img/trans.png)
[英]How can I set a timeout for Swagger generated python API client?
[英]How to set the Bearer token in the Python API client generated by Swagger Codegen 3.x?
我通过使用位于https://generator.swagger.io/的在线 Swagger Codegen 为这个 API生成了一个 Python 客户端库。 API使用Bearer认证:
openapi: 3.0.0
...
paths:
/sandbox/register:
post:
...
security:
- sso_auth: []
...
components:
securitySchemes:
sso_auth:
type: http
scheme: bearer
但是,生成的Python客户端中的Configuration
class没有access_token
字段。
使用生成的客户端库时如何填写Bearer access token?
codegen 端点POST /gen/clients/{language}
有authorizationValue
和securityDefinition
参数——我需要以某种方式配置这些参数吗?
"authorizationValue": {
"value": "string",
"type": "string",
"keyName": "string"
},
"securityDefinition": {
"type": "string",
"description": "string"
}
首先,由于您的 API 是 OpenAPI 3.0,因此您需要使用 Swagger Codegen 3.x,即https://generator3.swagger.io或swagger-codegen-cli-3.0.11.jar
位于https://generator.swagger.io的生成器用于 OpenAPI 2.0 ( swagger: '2.0'
)。
也就是说,Swagger Codegen 3.x 的 Python 生成器中存在一个错误,它不会生成 OpenAPI 3.0 定义中的 Bearer 身份验证代码。 请在https://github.com/swagger-api/swagger-codegen-generators/issues提交错误报告
/gen/clients
的authorizationValue
和securityDefinition
参数与 OpenAPI 文件中的安全定义无关。
作为解决方法,编辑您的 OpenAPI YAML 文件并替换这部分
securitySchemes:
sso_auth:
type: http
scheme: bearer
和:
securitySchemes:
sso_auth:
type: apiKey
in: header
name: Authorization
然后从修改后的 API 定义生成一个新的 Python 客户端。
现在,按照 README.md 中的说明安装客户端README.md
,您应该能够按如下方式设置令牌:
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))
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.