![](/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.