I have a requirement where I need to get an access token of a user.
I am aware of the admin username and password and hence can get the access token of the admin.
Is there any rest API that can provide me access token a user using the above data?
解决方案
There are two ways to get access token. One with Rest client (keycloak Rest API) and other through java keycloak-admin-client library.
1. Keycloak Rest API:
URI: http://keycloak:8080/auth/realms/myrealm/protocol/openid-connect/token
Type: POST
Content-Type: application/x-www-form-urlencoded
grant_type:password
username:user
password:user_password
client_id:client_id
secret_id:client_secret
2. Keycloak admin client (JAVA)
Keycloak instance = Keycloak.getInstance("http://keycloak:8080/auth", "myrealm", "user", "user_password","client_id", "client_secret");
TokenManager tokenmanager = instance.tokenManager();
String accessToken = tokenmanager.getAccessTokenString();