How to pass multiple authorization keys to swagger-js?

741 views Asked by At

Using swagger-ui version 2.1.0 I'm trying to pass multiple authorization params to the URL requested. Looking at the example given on github I've tried:

var api_key = 'api_key',
    username = 'username';

swaggerUi.api.clientAuthorizations.add('api_key', new SwaggerClient.ApiKeyAuthorization('api_key', api_key, 'query'));
swaggerUi.api.clientAuthorizations.add('username', new SwaggerClient.ApiKeyAuthorization('username', username, 'query'));

But this results in only the first one of them being passed to the XHR, e.g.:

http://127.0.0.1:8000/api/v1/capability?api_key=api_key

If I swap the order:

swaggerUi.api.clientAuthorizations.add('username', new SwaggerClient.ApiKeyAuthorization('username', username, 'query'));
swaggerUi.api.clientAuthorizations.add('api_key', new SwaggerClient.ApiKeyAuthorization('api_key', api_key, 'query'));

Then:

http://127.0.0.1:8000/api/v1/channel?username=username

I've also tried using the object notation in add:

swaggerUi.api.clientAuthorizations.add({
    'api_key': new SwaggerClient.ApiKeyAuthorization("api_key", api_key, "query"),
    'username': new SwaggerClient.ApiKeyAuthorization("username", username, "query")
});

But again only single parameter was passed:

http://127.0.0.1:8000/api/v1/capability?api_key=api_key

The name of the function suggests it should be possible to pass multiple authorization params without any collisions but the practice shows otherwise.

1

There are 1 answers

0
Peter Kilczuk On BEST ANSWER

The only way that seems to work for me is to bind a custom function to swaggerUi.api.clientAuthorizations.apply, e.g.:

    swaggerUi.api.clientAuthorizations.apply = function (obj) {
        obj.url += '?api_key=' + api_key + '&username=' + username;
    }

(the example assumes that api_key and username are present in the current scope)

swaggerUi.api.clientAuthorizations.apply takes two arguments obj, securities - it's the first one you'll want to use to attach credentials to your HTTP requests.