[英]How to set the Bearer token in the Python API client generated by Swagger Codegen 3.x?
I've generated a Python client library for this API by using the online Swagger Codegen at https://generator.swagger.io/ .我通过使用位于https://generator.swagger.io/的在线 Swagger Codegen 为这个 API生成了一个 Python 客户端库。 The API uses Bearer authentication:
API使用Bearer认证:
openapi: 3.0.0
...
paths:
/sandbox/register:
post:
...
security:
- sso_auth: []
...
components:
securitySchemes:
sso_auth:
type: http
scheme: bearer
However, the Configuration
class in generated Python client has no access_token
field.但是,生成的Python客户端中的
Configuration
class没有access_token
字段。
How to fill the Bearer access token when using the generated client library?使用生成的客户端库时如何填写Bearer access token?
The codegen endpoint POST /gen/clients/{language}
has the authorizationValue
and securityDefinition
parameters - do I need to configure these parameters somehow? codegen 端点
POST /gen/clients/{language}
有authorizationValue
和securityDefinition
参数——我需要以某种方式配置这些参数吗?
"authorizationValue": {
"value": "string",
"type": "string",
"keyName": "string"
},
"securityDefinition": {
"type": "string",
"description": "string"
}
First of all, since your API is OpenAPI 3.0 you need to use Swagger Codegen 3.x, ie https://generator3.swagger.io or swagger-codegen-cli-3.0.11.jar
.首先,由于您的 API 是 OpenAPI 3.0,因此您需要使用 Swagger Codegen 3.x,即https://generator3.swagger.io或
swagger-codegen-cli-3.0.11.jar
The generator at https://generator.swagger.io is for OpenAPI 2.0 ( swagger: '2.0'
).位于https://generator.swagger.io的生成器用于 OpenAPI 2.0 (
swagger: '2.0'
)。
That said, there's a bug in the Python generator of Swagger Codegen 3.x, it doesn't generate the code for Bearer authentication in OpenAPI 3.0 definitions.也就是说,Swagger Codegen 3.x 的 Python 生成器中存在一个错误,它不会生成 OpenAPI 3.0 定义中的 Bearer 身份验证代码。 Please file a bug report at https://github.com/swagger-api/swagger-codegen-generators/issues
请在https://github.com/swagger-api/swagger-codegen-generators/issues提交错误报告
The authorizationValue
and securityDefinition
parameters of /gen/clients
are not related to security definitions in OpenAPI files. /gen/clients
的authorizationValue
和securityDefinition
参数与 OpenAPI 文件中的安全定义无关。
As a workaround, edit your OpenAPI YAML file and replace this part作为解决方法,编辑您的 OpenAPI YAML 文件并替换这部分
securitySchemes:
sso_auth:
type: http
scheme: bearer
with:和:
securitySchemes:
sso_auth:
type: apiKey
in: header
name: Authorization
Then generate a new Python client from the modified API definition.然后从修改后的 API 定义生成一个新的 Python 客户端。
Now, once you have installed the client package as explained in README.md
, you should be able to set the token as follows:现在,按照 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))
...
In my case I could simply set access_token
of Configuration
object like this:在我的例子中,我可以像这样简单地设置
Configuration
object 的access_token
:
configuration = Configuration()
configuration.verify_ssl = ...
configuration.host = ...
configuration.access_token = token
Information taken from here, schema generated by fastapi.从这里获取的信息,由 fastapi 生成的模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.