简体   繁体   English

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

[英]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}authorizationValuesecurityDefinition参数——我需要以某种方式配置这些参数吗?

"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.ioswagger-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/clientsauthorizationValuesecurityDefinition参数与 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.

相关问题 如何为 Swagger 生成的 python API 客户端设置超时? - How can I set a timeout for Swagger generated python API client? 如何在openapi-codegen生成的python代码中使用承载认证 - How to use bearer authentication in openapi-codegen generated python code 在python中使用swagger codegen客户端向api调用添加标头的细节尚不清楚 - The specifics of adding a header to an api call with a swagger codegen client in python are unclear 如何将接收到的(承载)访问令牌传递给生成的 REST 客户端以调用安全的 API-Gateway 端点 - How to pass received (bearer) access token to generated REST Client in order to invoke secured API-Gateway Endpoint 如何在 python api 中传递不记名令牌 - how to pass bearer token in python api 如何获取和使用 Dropbox API (Python 3.x) 的刷新令牌 - How do you get and use a Refresh Token for the Dropbox API (Python 3.x) Flask Rest API - 如何在 python 请求中使用 Bearer API 令牌 - Flask Rest API - How to use Bearer API token in python requests Python 3.x的Hive客户端 - Hive client for Python 3.x Python gRPC 客户端请求仅带有不记名令牌 - Python gRPC client request only with bearer token 客户端(1)到Python 3.x中的服务器到客户端(2)(套接字?) - Client(1) to Server to Client(2) in Python 3.x (Sockets?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM