OpenAPI & Python: 如何使用 apiKey 安全方案?
OpenAPI & Python: How to use apiKey security scheme?
我正在尝试通过 Python 使用具有 OpenAPI 规范的 API。我生成了 openapi_client 并使用生成的示例之一开始。对 API 的第一次调用成功,但由于 session ID 无效,后续调用失败。
securitySchemes:
user_session_authentication:
description: [...]
type: apiKey
in: header
name: SOME_PREFIX_user_session_id
security:
- user_session_authentication: [] # default for all actions is user_session_id!
import openapi_client
from openapi_client.api import some_api
from openapi_client.model.inline_response200 import InlineResponse200
from openapi_client.model.inline_response2002 import InlineResponse2002
from pprint import pprint
with openapi_client.ApiClient() as api_client:
username = "john"
password = "45d75ii47"
api_instance = some_api.SomeApi(api_client)
username = username
password = password
body = function_a("foo")
api_response = api_instance.foo(username=username, password=password, body=body)
pprint(api_response)
api_response = api_instance.bar()
pprint(api_response)
对 foo() 的 http 响应包含 key-value 对 'user_session_id': '1bf92fc3-7f2a-450f-a472-03a07a70bd8d'
。如何将此 key-value 对添加到第二个请求的 header 中?目前,第二个请求的 header 仅包含以下内容:{'Accept': 'application/json', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}
我发现了 个问题并针对我的用例调整了代码:
import openapi_client
from openapi_client.api import some_api
from openapi_client import Configuration
from openapi_client.model.inline_response200 import InlineResponse200
from openapi_client.model.inline_response2002 import InlineResponse2002
from pprint import pprint
with openapi_client.ApiClient() as api_client:
username = "john"
password = "45d75ii47"
api_instance = some_api.SomeApi(api_client)
username = username
password = password
body = function_a("foo")
api_response = api_instance.foo(username=username, password=password, body=body)
pprint(api_response)
conf = Configuration()
conf.api_key = {"SOME_PREFIX_user_session_id": api_response.user_session_id}
api_client = openapi_client.ApiClient(None, "SOME_PREFIX_user_session_id", conf.get_api_key_with_prefix("user_session_id"))
api_instance = some_api.SomeApi(api_client)
api_response = api_instance.bar()
pprint(api_response)
我正在尝试通过 Python 使用具有 OpenAPI 规范的 API。我生成了 openapi_client 并使用生成的示例之一开始。对 API 的第一次调用成功,但由于 session ID 无效,后续调用失败。
securitySchemes:
user_session_authentication:
description: [...]
type: apiKey
in: header
name: SOME_PREFIX_user_session_id
security:
- user_session_authentication: [] # default for all actions is user_session_id!
import openapi_client
from openapi_client.api import some_api
from openapi_client.model.inline_response200 import InlineResponse200
from openapi_client.model.inline_response2002 import InlineResponse2002
from pprint import pprint
with openapi_client.ApiClient() as api_client:
username = "john"
password = "45d75ii47"
api_instance = some_api.SomeApi(api_client)
username = username
password = password
body = function_a("foo")
api_response = api_instance.foo(username=username, password=password, body=body)
pprint(api_response)
api_response = api_instance.bar()
pprint(api_response)
对 foo() 的 http 响应包含 key-value 对 'user_session_id': '1bf92fc3-7f2a-450f-a472-03a07a70bd8d'
。如何将此 key-value 对添加到第二个请求的 header 中?目前,第二个请求的 header 仅包含以下内容:{'Accept': 'application/json', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}
我发现了
import openapi_client
from openapi_client.api import some_api
from openapi_client import Configuration
from openapi_client.model.inline_response200 import InlineResponse200
from openapi_client.model.inline_response2002 import InlineResponse2002
from pprint import pprint
with openapi_client.ApiClient() as api_client:
username = "john"
password = "45d75ii47"
api_instance = some_api.SomeApi(api_client)
username = username
password = password
body = function_a("foo")
api_response = api_instance.foo(username=username, password=password, body=body)
pprint(api_response)
conf = Configuration()
conf.api_key = {"SOME_PREFIX_user_session_id": api_response.user_session_id}
api_client = openapi_client.ApiClient(None, "SOME_PREFIX_user_session_id", conf.get_api_key_with_prefix("user_session_id"))
api_instance = some_api.SomeApi(api_client)
api_response = api_instance.bar()
pprint(api_response)