Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]add authorization button in swagger ui #215

Open
johnny-cy opened this issue Mar 15, 2021 · 3 comments
Open

[Question]add authorization button in swagger ui #215

johnny-cy opened this issue Mar 15, 2021 · 3 comments

Comments

@johnny-cy
Copy link

I'm using FlaskApiSpec, but I couldn't make the authorization button appear in swagger ui,

I've tried the following solution but it didn't work,
https://mattfrear.com/2018/07/21/add-an-authorization-header-to-your-swagger-ui-with-swashbuckle-revisited/

here are my config

from flask import Flask, Blueprint
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask_apispec import FlaskApiSpec

def create_app(mode='dev', test_configs=None):
    app = Flask(__name__)
    app.extensions['mode'] = mode
    marshmallow_plugin = MarshmallowPlugin()

    # Filter the warnins for partial schemas 
    warnings.filterwarnings("ignore", message="Multiple schemas resolved to the name ")
    authorizations = {
        'apikey': {
            'type': 'apiKey',
            'in': 'header',
            'name': 'X-API-KEY'
        }
    }

    app.config.update({
        ## flask-apispec
        'APISPEC_SPEC': APISpec(
            title='DeepCT Notifier Server APIs',
            version='v1.0.0',
            plugins=[marshmallow_plugin],
            openapi_version='2.0',
            authorizations=authorizations
        ),
        'APISPEC_SWAGGER_UI_URL': '/api-spec',  # Define the api spec route
       

    marshmallow_plugin.converter.add_attribute_function(_enum_to_properties)    # Support showing enum on Swagger

    docs = FlaskApiSpec(app, document_options=False)

    return app

and I also tried put authorizations variables at FlaskApiSpec like FlaskApiSpec(app, document_options=False, authorizations=authorizations). but it ended it is unexpected key word.

Does flask-apispec support to get authorized via button a head of the swagger ui page instead of manually set "Authorization" to Bearer in individual endpoint's headers ?

thanks for helping

@CineXWorm
Copy link

CineXWorm commented Feb 3, 2023

This works using:

spec = APISpec(
        title='PlaceHolder',
        version='v1',
        plugins=[MarshmallowPlugin()],
        openapi_version='2.0.0')

api_key_scheme = {"type": "apiKey", "in": "header", "name": "X-API-Key"}
spec.components.security_scheme("ApiKeyAuth", api_key_scheme)

app.config.update({
    'APISPEC_SPEC': spec,
    'APISPEC_SWAGGER_URL': '/swagger/',  # URI to access API Doc JSON
    'APISPEC_SWAGGER_UI_URL': '/swagger-ui/'  # URI to access UI of API Doc
})

@lety-cruz
Copy link

lety-cruz commented Apr 14, 2023

@CineXWorm I think that, even though this adds the green Authorize button, it does not add the security header to the request: curl -X GET "http://127.0.0.1:5000/" -H "accept: application/json"

image

@freetsi
Copy link

freetsi commented Jan 5, 2024

Hello! The solution is here #248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants