Forum Discussion

gboscaro's avatar
New Contributor
4 years ago

Swagger Codegen - Cannot make OAuth work

I am using the Swagger Generator website to generate the code for an API in Java. The API specifications are public and available here. As you can see, seems like the JSON doesn't have any Authentication configuration in it, but the API actually uses an OAuth authentication using "Microsoft Identity Platform" as authority and using an Application ID and Secret to get a JWT. I can obtain the token in Java using external libraries, but I'd like to integrate the process in Swagger as much as possible.


By importing into the generator the previous JSON, obviously no Authorization is configured and all API calls fail. I modified the JSON and added these parameters:



            "write:protocollo":"Modify Protocolli",
            "read:protocollo":"Read Protocolli"



And then, for every Path I added






I generated all the files and imported them correctly in my project, but I still could not manage to test an authenticated API call.

In particular:


  1. I don't know if my changes in the JSON are correct and if it is the right procedure. Am I missing something? Am I doing something wrong?
  2. I do not know the right way to handle the authentication and API calls in Java after generating the code. Right now I am doing like this:


String authority = "";
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authority, true, service);
Future<AuthenticationResult> future = context.acquireToken(
  new ClientCredential(
AuthenticationResult result = future.get();	
OAuth authentication = new OAuth();
ApiClient apiClient = new ApiClient();		
apiClient.getAuthentications().put("oauth2", authentication);
ProtocolloApi api = new ProtocolloApi();
Call call = api.protocolloGetTipoPostaCall("1.0", null, null);
Response response = call.execute();



But since I am missing experience and documentation, this is just improvisation.

Can you help me?


Best regards

3 Replies

  • Jayde's avatar
    Occasional Visitor

    You don't need React specific code generation. If you use swagger-codegen v2.4.x you can generate Typescript for a bunch of flavours like Angular1.x, Angular2.x, Fetch, jQuery, Node. Only v3.x supports Angular only. I don't know what it does that's Angular specific but maybe dependency injection related?! I don't know.