gpt4 book ai didi

python - 如何在 Swagger Codegen 3.x 生成的 Python API 客户端中设置 Bearer token ?

转载 作者:行者123 更新时间:2023-11-30 21:55:00 25 4
gpt4 key购买 nike

我已经为 this API 生成了一个 Python 客户端库使用在线 Swagger Codegen https://generator.swagger.io/ 。 API 使用 Bearer 身份验证:

openapi: 3.0.0
...

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

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

但是,生成的 Python 客户端中的 Configuration 类没有 access_token 字段。

使用生成的客户端库时如何填写 Bearer 访问 token ?


codegen 端点 POST/gen/clients/{language} 具有 authorizationValuesecurityDefinition 参数 - 我需要以某种方式配置这些参数吗?

"authorizationValue": {
"value": "string",
"type": "string",
"keyName": "string"
},
"securityDefinition": {
"type": "string",
"description": "string"
}

最佳答案

首先,由于您的 API 是 OpenAPI 3.0,您需要使用 Swagger Codegen 3.x,即 https://generator3.swagger.ioswagger-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 提交错误报告

authorizationValuesecurityDefinition /gen/clients的参数与 OpenAPI 文件中的安全定义无关。


作为解决方法,请编辑您的 OpenAPI YAML 文件并替换此部分

  securitySchemes:
sso_auth:
type: http
scheme: bearer

与:

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

然后根据修改后的 API 定义生成新的 Python 客户端。

现在,一旦您按照 README.md 中的说明安装了客户端软件包, ,您应该能够按如下方式设置 token :

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))
...

关于python - 如何在 Swagger Codegen 3.x 生成的 Python API 客户端中设置 Bearer token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com