Created
May 7, 2020 18:39
-
-
Save raviyasas/84cd45f9b2df9a3e0b9a781d896fb9d2 to your computer and use it in GitHub Desktop.
Keycloak Admin REST APIs - Create new user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import java.io.*; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.ArrayList; | |
public class CreateNewUser implements Serializable { | |
public static void main(String args[]) throws IOException { | |
new CreateNewUser().createUser("user1", "üser1@gmail.com", "user1"); | |
} | |
public int createUser(String username, String email, String password) throws IOException { | |
String grant_type = "password"; | |
String port = "8081"; | |
String realm = "api"; | |
String server = "localhost"; | |
String content_type = "application/json"; | |
String method_type = "POST"; | |
String encode_type = "UTF-8"; | |
String bearerToken = new GetToken().getBearerToken(); | |
int responseCode = 0; | |
ArrayList arrayList = new ArrayList(); | |
Credentials credentials = new Credentials(); | |
if (grant_type != null) { | |
credentials.setType(grant_type); | |
} | |
credentials.setTemporary(true); | |
if (password != null) { | |
credentials.setValue(password); | |
} | |
arrayList.add(credentials); | |
PostBody postBody = new PostBody(); | |
if (username != null) { | |
postBody.setUsername(username); | |
} | |
if (email != null) { | |
postBody.setEmail(email); | |
} | |
postBody.setCredentials(arrayList); | |
try { | |
Gson gson = new Gson(); | |
String jsonString = gson.toJson(postBody); | |
URL url = new URL("http://" + server + ":" + port + "/auth/admin/realms/" + realm + "/users/"); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod(method_type); | |
conn.setRequestProperty("Content-Type", content_type); | |
conn.setRequestProperty("Accept", content_type); | |
conn.setDoOutput(true); | |
conn.setRequestProperty("Authorization", "Bearer " + bearerToken.trim()); | |
OutputStream os = conn.getOutputStream(); | |
byte[] input = jsonString.getBytes(encode_type); | |
os.write(input, 0, input.length); | |
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), encode_type)); | |
StringBuilder response = new StringBuilder(); | |
String responseLine = null; | |
while ((responseLine = br.readLine()) != null) { | |
response.append(responseLine.trim()); | |
} | |
responseCode = conn.getResponseCode(); | |
System.out.println(responseCode); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
return responseCode; | |
} | |
public class PostBody implements Serializable { | |
private String email; | |
private String username; | |
private ArrayList credentials; | |
public PostBody() { | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public ArrayList getCredentials() { | |
return credentials; | |
} | |
public void setCredentials(ArrayList credentials) { | |
this.credentials = credentials; | |
} | |
} | |
public class Credentials implements Serializable { | |
private String value; | |
private Boolean temporary; | |
private String type; | |
public Credentials() { | |
} | |
public String getValue() { | |
return value; | |
} | |
public void setValue(String value) { | |
this.value = value; | |
} | |
public Boolean getTemporary() { | |
return temporary; | |
} | |
public void setTemporary(Boolean temporary) { | |
this.temporary = temporary; | |
} | |
public String getType() { | |
return type; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment