Keycloak keycloak = KeycloakBuilder.builder()
.serverUrl(“http://localhost:8080”)
.grantType(OAuth2Constants.PASSWORD)
.realm(“master”)
.clientId(“admin-cli”)
.username(“admin”)
.password(“admin”)
.resteasyClient(
new ResteasyClientBuilderImpl()
.connectionPoolSize(10).build()
)
.build();
keycloak.tokenManager().getAccessToken();
RealmResource realmResource = keycloak.realm(“hello-world-authz”);
ClientsResource clients = realmResource.clients();
ClientResource clientResource = clients.get(“my-resource-server”);AuthorizationResource authorization = clientResource.authorization(); ResourceScopesResource scopes = authorization.scopes(); ScopeRepresentation newScope = new ScopeRepresentation("create"); scopes.create(newScope);
Above code results in:
17:48:11.092 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.14)
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “POST /admin/realms/hello-world-authz/clients/my-resource-server/authz/resource-server/scope HTTP/1.1[\r][\n]”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “Accept: application/json[\r][\n]”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “Authorization: Bearer ”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “Content-Type: application/json[\r][\n]”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “Content-Length: 94[\r][\n]”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “Host: localhost:8080[\r][\n]”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “Connection: Keep-Alive[\r][\n]”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.14)[\r][\n]”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “[\r][\n]”
17:48:11.092 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> “{“id”:null,“name”:“create”,“iconUri”:null,“policies”:null,“resources”:null,“displayName”:null}”
17:48:11.100 [main] DEBUG org.apache.http.wire - http-outgoing-0 << “HTTP/1.1 404 Not Found[\r][\n]”
Please let me know if I am missing something.