Swagger-ui: swagger 2.0 apikey authorization

Created on 30 Jun 2017  路  8Comments  路  Source: swagger-api/swagger-ui

I am trying to set api key but the Swagger 2.0 UI does not reflect the header info in the curl.

window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "http://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
onComplete: function(swaggerApi, swaggerUi, initOAuth){
$("pre code").each(function(i, e) {hljs.highlightBlock(e)});
var authKey = new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer d88fb9b5-8625-4731-85db-99d6f2647a2e', 'header');
window.swaggerUi.api.clientAuthorizations.add("api_key", authKey);
},
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
}

lock-bot

Most helpful comment

I've the same issue to solve. What is my problem?
`window.onload = function () {

    // Build a system
    const ui = SwaggerUIBundle({
        urls: [
            {url: "http://localhost:8080/v2/api-docs?group=api", name: "api"},
            {url: "http://localhost:8080/v2/api-docs?group=admin-api", name: "admin api"}
        ],
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
            SwaggerUIBundle.presets.apis,
            SwaggerUIStandalonePreset
        ],
        plugins: [
            SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout",
        securityDefinitions: {
            JWT: {
                type: "apiKey",
                description: "",
                name: "Authorization",
                in: "header"
            }
        }
    });

    ui.authActions.authorize({
        JWT: {
            name: "JWT",
            schema: {type: "apiKey", in: "header", name: "Authorization", description: ""},
            value: "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbkBpY29uZWN0LmlvIiwiYXVkaWVuY2UiOiJ3ZWIiLCJjcmVhdGVkIjoxNTAwOTk1MTk4NDM2LCJleHAiOjE1MDE1OTk5OTh9.KwkUcQ1hYB3FWS_rlhyaKUo1x84ZRuXKbukZOkEDeSU1uUpgpNuvrAfKlJsgmAYLwxUUU_gIeOKsYekHEGm2iQ"
        }
    });
    window.ui = ui
}`

SwaggerUI says still: curl -X GET "http://localhost:8080/api/contact/" -H "accept: /" without any header information.

All 8 comments

I init my Swagger UI
const ui = SwaggerUIBundle({ url: '', dom_id: jqDom_id, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout", validatorUrl: null, operationsSorter: "alpha" }) window.ui = ui

When I load the page with the swagger UI documentation, I preload my authorization value into an HTML input field and the I add the api_key authorization header in this way
ui.authActions.authorize({api_key: {name: "api_key", schema: {type: "apiKey", in: "header", name: "X-My-Header-Name", description: ""}, value: myAuthorizationValue}});

In some cases I manage also the logout in this way
ui.authActions.logout(["api_key"]);

Thanks for the reply. I tried using ui.authAction.authorize but it still doesn't take effect.
Am I missing any js files? Or something else?

window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "http://localhost:9080/preferences/api-docs",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
var authKey = "Bearer b78fb6b5-8625-4731-85db-99y6f2647a2e";
ui.authActions.authorize({api_key: {name: "api_key", schema: {type: "apiKey", in: "header", name: "Authorization", description: ""}, value: authKey}});
}

Maybe depends on the wrong name of your authorization spec? Are you sure that is 'api_key'?
Try to have a look at the issue #2915 (the third comment of fernandocamargoti)

Thanks zem83. I couldn't get the Header Bearer token to work but the access_token with query works for now with ui.authActions.authorize().
I should be able to get the header working too. Something to do with my securityDefinitions I guess.

Good :)

Make sure api_key name is match what is defined in 'securityDefinitions'

I've the same issue to solve. What is my problem?
`window.onload = function () {

    // Build a system
    const ui = SwaggerUIBundle({
        urls: [
            {url: "http://localhost:8080/v2/api-docs?group=api", name: "api"},
            {url: "http://localhost:8080/v2/api-docs?group=admin-api", name: "admin api"}
        ],
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
            SwaggerUIBundle.presets.apis,
            SwaggerUIStandalonePreset
        ],
        plugins: [
            SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout",
        securityDefinitions: {
            JWT: {
                type: "apiKey",
                description: "",
                name: "Authorization",
                in: "header"
            }
        }
    });

    ui.authActions.authorize({
        JWT: {
            name: "JWT",
            schema: {type: "apiKey", in: "header", name: "Authorization", description: ""},
            value: "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbkBpY29uZWN0LmlvIiwiYXVkaWVuY2UiOiJ3ZWIiLCJjcmVhdGVkIjoxNTAwOTk1MTk4NDM2LCJleHAiOjE1MDE1OTk5OTh9.KwkUcQ1hYB3FWS_rlhyaKUo1x84ZRuXKbukZOkEDeSU1uUpgpNuvrAfKlJsgmAYLwxUUU_gIeOKsYekHEGm2iQ"
        }
    });
    window.ui = ui
}`

SwaggerUI says still: curl -X GET "http://localhost:8080/api/contact/" -H "accept: /" without any header information.

Closing due to inactivity.

This is simply to keep our issue tracker clean - feel free to comment if there are any further thoughts or concerns, and we'll be happy to reopen this issue.

Was this page helpful?
0 / 5 - 0 ratings