0

I am bit new to Spring and Java, can someone please an provide example to call an API which takes OAuth access token with ApiClient which is auto-generated using swagger plugin? By providing client id, secret and oAuth Target Url in below sample code. Please advise.

ApiClient apiClient = new ApiClient();
apiClient.setBasePath(“http://localhost:8080”);
CustomersApi customerApi = new CustomersApi(apiClient);
List customers = customerApi.getCustomers(“peter”, 40);

Below is the sample code in this blog.

https://blog.philipphauer.de/enriching-restful-services-swagger/

Imran
  • 5,542
  • 3
  • 23
  • 46
  • 1
    You need to clarify a few things first. Which OAuth version are you using? Are you trying to write a Swagger or do you have a Swagger for an OAuth Api? From what i understand authentication and authorization is not part of the ApiClient – Ravindranath Akila Jan 18 '17 at 04:16
  • @RavindranathAkila I have an API which accepts oAuth2 access token(example - API hosted on Apigee). I am trying to call this API using from my code using swagger codegen maven plugin client. if I understand correctly, I can provide client id, secret to above api client so client will call oAuth service(Apigee) first and after successful token response, it will call the API. – Imran Jan 18 '17 at 20:15
  • Then you should share the chunk of code which configures OAuth. At least what you are trying. – Ravindranath Akila Jan 18 '17 at 22:48
  • @RavindranathAkila. ApiClient apiClient = new ApiClient("oAuth2"); apiClient.configureAuthorizationFlow("clientid","secret",""); I am bit new to Java, trying to under which methods I need to implement to pass oAuth details. Please advise – Imran Jan 19 '17 at 04:07
  • Being new to Java, OAuth call flow being a bit complicated and the auto generated library makes your whole objective quite tough to achieve. Let's break it down. 1. Are you familiar with OAuth? 2. Does the generated code have a documented way to configure OAuth. 3. What is your end game; as in what is the final code supposed to do? – Ravindranath Akila Jan 19 '17 at 08:09
  • This is my understanding. 1. oAuth2 exposed services required Access Token. 2. You can get access token, by calling oAuth service first by passing client id and secret 3. Right now, we have manually implemented the logic to call oAuth service as Rest call, get the token and add it as header to next micro service.(Authorization - Bearer xxx) 4. We want to move away from above manual code and start using above plugin. – Imran Jan 19 '17 at 13:47
  • Just had a look at the blog post and the plugin page. Looks like you're are confused with client and server code generation of various swagger tools. Some tools will generate the server mock code, and some will generate the client mock code. There's also tools to read an api and reverse generate a swagger but that's beside the point. Now, what you have here is the client code. That's all well, but who implemented the server compliment of the swagger spec? Did you generate it? There's no reference in the project saying it will auto generate an OAuth API for you. – Ravindranath Akila Jan 19 '17 at 14:44

1 Answers1

0

This is client side code. You need to generate and implement the server component of your swagger, and then configure the client accordingly.

Ravindranath Akila
  • 209
  • 3
  • 35
  • 45