I am using Spring Reactive to get access token for a registered OKTA app. I have Client Id and Secret with me along with the URI.
I would like to know how to get access token from the client Id and secret.
I don’t need the API hit approach here, please provide me some classes which can be used here like application context classes.
If possible, provide URL for reference.
This blog post shows you how to get an access token:
package com.oktadeveloper.graphqldemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
@RestController
class MyAccessTokenController {
@Autowired
private OAuth2AuthorizedClientService clientService;
@RequestMapping("/my-access-token")
String home(Principal user) {
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) user;
String authorizedClientRegistrationId = token.getAuthorizedClientRegistrationId();
String name = user.getName();
OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(authorizedClientRegistrationId, name);
return "token: " + client.getAccessToken().getTokenValue();
}
}
I don’t have a client, it should be server to server communication. My program has to create a token and not through API calls. I need to use only spring security classes to generate it. Probably a dependency from Okta for spring boot is what I needed. With article if possible.