Forum Discussion

gboscaro's avatar
gboscaro
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:

 

 

   "securityDefinition":{
      "civnext_oauth":{
         "type":"oauth2",
         "authorizationUrl":"https://login.microsoftonline.com/............./oauth2/token",
         "flow":"application",
         "scopes":{
            "write:protocollo":"Modify Protocolli",
            "read:protocollo":"Read Protocolli"
         }
      }
   },

 

 

And then, for every Path I added

 

 

            "security":{
               "civnext_oauth":[
                  "write:protocollo",
                  "read:protocollo"
               ]
            },

 

 

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 = "https://login.microsoftonline.com/............./oauth2/token";
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authority, true, service);
		
Future<AuthenticationResult> future = context.acquireToken(
  "api_resource_path", 
  new ClientCredential(
    "app_id", 
    "secret"), 
  null);
		
AuthenticationResult result = future.get();	
	
OAuth authentication = new OAuth();
authentication.setAccessToken(result.getAccessToken());
		
ApiClient apiClient = new ApiClient();		
apiClient.getAuthentications().put("oauth2", authentication);
apiClient.setBasePath("basepath");
apiClient.setAccessToken(result.getAccessToken());
		
ProtocolloApi api = new ProtocolloApi();
api.setApiClient(apiClient);
Call call = api.protocolloGetTipoPostaCall("1.0", null, null);
Response response = call.execute();
System.out.println(response.toString());

 

 

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

Can you help me?

 

Best regards

  • Jayde's avatar
    Jayde
    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.

     

     

     

     

     

    mymilestonecard.com