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:
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
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.
I am actully generating for Java
Subject | Author | Latest Post |
---|---|---|