Hi Jitendra, try this code snippet[1]. I hope it helps.
[1] -
https://gist.github.com/abstractj/e202d31d877552108e5448638872cd20
On 2016-07-26, Jitendra Chouhan wrote:
Thanks Paulo. Adding last two dependencies solved root problem.
I am trying to create new realm using below code snippet but getting error.
Please find code snippet and error stack at keycloak server console which
as follows:
Code Snippet:
Keycloak kc = KeycloakBuilder.builder() //
.serverUrl("http://localhost:8080/auth") //
.realm("master")//
.username("admin") //
.password("admin") //
.clientId("admin-cli") //
// .clientId("security-admin-console") //
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
//
.build();
RealmRepresentation realmRep = new RealmRepresentation();
realmRep.setDisplayName("Created_Realm");
realmRep.setEnabled(true);
kc.realms().create(realmRep);
Error Stack:
Caused by: java.lang.NullPointerException
at
org.keycloak.services.managers.RealmManager.createMasterAdminManagement(RealmManager.java:289)
at
org.keycloak.services.managers.RealmManager.setupMasterAdminManagement(RealmManager.java:281)
at
org.keycloak.services.managers.RealmManager.importRealm(RealmManager.java:433)
at
org.keycloak.services.resources.admin.RealmsAdminResource.importRealm(RealmsAdminResource.java:151)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at
org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
at
org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
at
org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
at
org.jboss.resteasy.core.ResourceLocatorInvoker.invokeOnTargetObject(ResourceLocatorInvoker.java:138)
at
org.jboss.resteasy.core.ResourceLocatorInvoker.invoke(ResourceLocatorInvoker.java:101)
at
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:395)
... 37 more
Upon looking into RealmManger source code at git hub found there is a
conditional check kept which compare realmName and adminRealm from
configuration
if (realm.getName().equals(Config.getAdminRealm())) inside
setupMasterAdminManagement
method
If we look above code snippet only realmDisplayName and enable attributes
are set during creation of realm(generally use to provide if create realm
using keycloak server console)
but conditional check on getName(name) of RealmModel class which might be
causing issue, there is no setName method(setter) available inside
RealmRepresentation class under keycloak client admin library.
Can someone please provide guidance on this issue or suggest a way to
create realm using keycloak-admin-client bundle.
Thanks,
Jitendra Chouhan
On Tue, Jul 26, 2016 at 1:53 PM, Paulo Pires <pires(a)littlebits.cc> wrote:
> You need to explicitly provide the following dependencies:
>
> <dependency>
> <groupId>org.keycloak</groupId>
> <artifactId>keycloak-admin-client</artifactId>
> <version>${keycloak.version}</version>
> </dependency>
> <dependency>
> <groupId>org.jboss.resteasy</groupId>
> <artifactId>resteasy-client</artifactId>
> <version>${resteasy.version}</version>
> </dependency>
> <dependency>
> <groupId>org.jboss.resteasy</groupId>
> <artifactId>resteasy-jackson2-provider</artifactId>
> <version>${resteasy.version}</version>
> </dependency>
> <dependency>
> <groupId>org.jboss.resteasy</groupId>
> <artifactId>resteasy-multipart-provider</artifactId>
> <version>${resteasy.version}</version>
> </dependency>
>
> Pires
>
> On Tue, Jul 26, 2016 at 8:08 AM Jitendra Chouhan <
> jitendrachouhan03(a)gmail.com> wrote:
>
>> I am using keycloak-admin-client jar for provisioning users in keycloak
>> server instance but facing issue if using keycloak-admin-client version
>> higher than 1.8.0.Final. Please find detailed informations about issue and
>> configurations used to provision user as below:
>>
>> 1) keycloak-server version: 1.9.8.Final or 2.0.0.Final
>> 2) keycloak-admin-client version: 1.9.8.Final or 2.0.0.Final
>>
>> Sample Code Snippet:
>>
>> Keycloak kc = KeycloakBuilder.builder() //
>> .serverUrl("http://localhost:8080/auth") //
>> .realm("master")//
>> .username("admin") //
>> .password("admin") //
>> .clientId("admin-cli") //
>> // .clientId("security-admin-console") //
>> .resteasyClient(new
>> ResteasyClientBuilder().connectionPoolSize(10).build())//
>> .build();
>> // 1. User
>> CredentialRepresentation credential = new CredentialRepresentation();
>> credential.setType(CredentialRepresentation.PASSWORD);
>> credential.setValue("test123");
>> credential.setTemporary(false);
>>
>> UserRepresentation user = new UserRepresentation();
>> user.setUsername("testuser");
>> user.setFirstName("Test");
>> user.setLastName("User");
>> user.setCredentials(asList(credential));
>> user.setEnabled(true);
>> user.setRealmRoles(asList("admin"));
>>
>> // create a user
>> //Response result = kc.realm("master").users().create(user);
>> Response result = kc.realm("demo").users().create(user);
>> if (result.getStatus() != 201) {
>> if(result.getStatus() == 409){
>> System.err.println("Couldn't create user since user already
exist.");
>> System.exit(0);
>> }
>> System.err.println("Couldn't create user.");
>> System.exit(0);
>> }
>> System.out.println("test user created.... verify in keycloak!");
>>
>> Error stack trace::
>>
>> Exception in thread "main"
>> javax.ws.rs.client.ResponseProcessingException:
>> javax.ws.rs.ProcessingException:
>> org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized
>> field "access_token" (Class
>> org.keycloak.representations.AccessTokenResponse), not marked as ignorable
>> at [Source: org.apache.http.conn.EofSensorInputStream@6f3c660a; line:
>> 1, column: 18] (through reference chain:
>> org.keycloak.representations.AccessTokenResponse["access_token"])
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:140)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:58)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:104)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:62)
>> at com.sun.proxy.$Proxy19.grantToken(Unknown Source)
>> at
>> org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:85)
>> at
>>
org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:65)
>> at
>>
org.keycloak.admin.client.token.TokenManager.getAccessTokenString(TokenManager.java:60)
>> at
>>
org.keycloak.admin.client.resource.BearerAuthFilter.filter(BearerAuthFilter.java:52)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:384)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:102)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:62)
>> at com.sun.proxy.$Proxy27.create(Unknown Source)
>> at
>>
com.samba.security.keycloak.KeycloakAdminClientExample.main(KeycloakAdminClientExample.java:79)
>> Caused by: javax.ws.rs.ProcessingException:
>> org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized
>> field "access_token" (Class
>> org.keycloak.representations.AccessTokenResponse), not marked as ignorable
>> at [Source: org.apache.http.conn.EofSensorInputStream@6f3c660a; line:
>> 1, column: 18] (through reference chain:
>> org.keycloak.representations.AccessTokenResponse["access_token"])
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:282)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181)
>> at
>> org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:211)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:104)
>> ... 13 more
>> Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException:
>> Unrecognized field "access_token" (Class
>> org.keycloak.representations.AccessTokenResponse), not marked as ignorable
>> at [Source: org.apache.http.conn.EofSensorInputStream@6f3c660a; line:
>> 1, column: 18] (through reference chain:
>> org.keycloak.representations.AccessTokenResponse["access_token"])
>> at
>>
org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
>> at
>>
org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
>> at
>>
org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
>> at
>>
org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
>> at
>>
org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
>> at
>>
org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
>> at
>>
org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
>> at
>>
org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
>> at
>> org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
>> at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
>> at
>>
org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
>> at
>>
org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:59)
>> at
>>
org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:51)
>> at
>>
org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
>> at
>>
org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
>> at
>>
org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:248)
>>
>> Same sample code with keycloak-admin-client version: 1.8.0 is working
>> fine and able to provision user in keycloak server. Hence there is some
>> issue with higher version of keycloak-client-admin bundle than 1.8.0.
>> Please find attached pom file for project.
>>
>> Thanks,
>> Jitendra Chouhan
>>
>> _______________________________________________
>> keycloak-user mailing list
>> keycloak-user(a)lists.jboss.org
>>
https://lists.jboss.org/mailman/listinfo/keycloak-user
>
>
_______________________________________________
keycloak-user mailing list
keycloak-user(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/keycloak-user
--
abstractj
PGP: 0x84DC9914