Swagger-js authentication in browser

1,156 views
Skip to first unread message

Chantal Gribaumont

unread,
Apr 24, 2014, 12:16:02 PM4/24/14
to swagger-sw...@googlegroups.com
Hello,

I'm trying to use the JS client from a HTML page. Here's my code :

<!DOCTYPE html>
<html>
  <head>
    <title>swagger.js example</title>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src='./lib/shred.bundle.js' type='text/javascript'></script>
    <script type="text/javascript" src="./lib/swagger.js"></script>
    <script type="text/javascript">
window.swagger = new SwaggerApi({
    success: function() {
      if(swagger.ready === true) {
swagger.apis.location.getLocation({siteKey:"redmarket", externalId:"12"}, function(data) {
          document.getElementById("data").innerHTML = data.content.data;
        });
      }
    }
  });
    </script>

  </head>
  <body>
  <div id="data">
  </div>
  </body>
</html>

How can I add a basicAuth authentication ? I've tried several things and nothing seems to work.

I've tried to add some http headers in the parameter I give my api method :

swagger.apis.location.getLocation({headers:{"Authorization", "Basic XXX"}, siteKey:"redmarket", externalId:"12"}, function(data) {
          document.getElementById("mydata").innerHTML = data.content.data;
});

I've tried to add authorization to the window

window.authorizations.add("basicAuth", new PasswordAuthorization("basicAuth", "test", "XXX"));

And some other (increasingly stupid) solutions. Nothing works. 

Any thoughts ? 

Thanks !

tony tam

unread,
Apr 25, 2014, 11:34:39 AM4/25/14
to swagger-sw...@googlegroups.com
Your second example:

window.authorizations.add("basicAuth", new PasswordAuthorization("basicAuth", "test", "XXX"));

Should work fine.  What version of swagger-ui are you using?  If you look in the network inspector on your browser, what do you see?

Chantal Gribaumont

unread,
May 2, 2014, 5:59:16 AM5/2/14
to swagger-sw...@googlegroups.com
Hello,

Thanks for your answer. Knowing that I was on the right track, I made several more tests using

window.authorizations.add("basicAuth", new PasswordAuthorization("basicAuth", "test", "XXX"));

It still doesn't work. Obviously, my Authorization header is not sent with the request to my API, so I get a 401 (Unauthorized) error in my JS console. And there's more ! It seems that the request is sent as on OPTIONS request, instead of a GET request. I have no idea why...

Here's the latest code I use :

<!DOCTYPE html>
<html>
  <head>
    <title>swagger.js example</title>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src='./lib/shred.bundle.js' type='text/javascript'></script>
    <script type="text/javascript" src="./lib/swagger.js"></script>
    <script type="text/javascript">
window.swagger = new SwaggerApi({
   success: function() {
     if(swagger.ready === true) {
       window.authorizations.add("basicAuth", new PasswordAuthorization("basicAuth", "test", "xxxx"));

       swagger.apis.location.getLocation({siteKey:"redmarket", externalId:"12"}, function(data) {
         document.getElementById("data").innerHTML = data.content.data;
       });
     }
   }
 });
    </script>

  </head>
  <body>
 <div id="data">
 </div>
  </body>
</html>


I don't use swagger-ui but swagger-js. The last available version.


Calling the API via node.js works fine so this must have to do with swagger-js and not my API.

tony tam

unread,
May 2, 2014, 11:55:16 AM5/2/14
to swagger-sw...@googlegroups.com
Hi, I'm stumped.  If I open a debug window while looking at the petstore:


And add an authorization:

window.authorizations.add("basicAuth", new PasswordAuthorization("basicAuth", "test", "xxxx"));

Then make a call:

window.swaggerUi.api.pet.getPetById({petId:"1"})


I see the header being sent, as expected:

  1. Authorization:
    Basic dGVzdDp4eHh4

curl -X GET http://petstore.swagger.wordnik.com:80/api/pet/1 -H "User-Agent: Shred" -H "Accept: application/json" -H "Authorization: Basic dGVzdDp4eHh4"

Can you try that in your instance of swagger ui?

steven thein

unread,
May 6, 2014, 11:27:10 AM5/6/14
to swagger-sw...@googlegroups.com
Hi Tony,

Is there any documentation on how to use swagger.js on the client side and how to use com.wordnik.swagger.jaxrs.listing.ApiListing on the server side?

Thanks
Steve

tony tam

unread,
May 6, 2014, 12:46:35 PM5/6/14
to swagger-sw...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages