Oauth2Flows example with OpenAPI generator

Are there any good examples of performing an oauth2 flow?

I can't for the life of me find any information on how to actually do this.

For reference, I'm using the OpenAPI generator with PetFinder's OpenAPI spec which uses the client credentials oauth2 flow (which can be found at the bottom of the page here )

I haven't used it myself, so can't vouch for it, but seems someone got it working with various APIs here: GitHub - luomein/authenticate-any-api

The page you linked shows an example of how to use your secrets to get a token using curl and how subsequent requests should provide this token in a request header.

I suggest you take a look at the example authentication middleware in the generator repo examples directory, which shows how to add a token to each request.

You’ll need to extend this solution to make the request if the token you have has expired and cache the new one.

I think you’re misunderstanding my original question.

I am aware of how to implement middleware that will add the access token to all my requests.

The problem is that the generated open api code doesn’t seem to expose any way for me to actually fetch the access token with my client secret and api key.

I am not that well versed in the openapi spec, but I think maybe Petfinder’s provided openapi spec is missing the access token endpoint.

Apologies for not understanding your question fully.

You’re right that the generated client doesn’t have a way of doing this for you.

The security key in the OpenAPI document is where API authors can include authentication methods. This document does state oauth2 but our generator doesn’t currently generate anything for this.

Your only option today is to hand roll this request in the middleware. Or provide the token by some other out of band method.

1 Like