use of com.okta.sdk.resource.ResourceException in project okta-sdk-java by okta.
the class Quickstart method main.
public static void main(String[] args) {
try {
// Instantiate a builder for your Client. If needed, settings like Proxy and Caching can be defined here.
ClientBuilder builder = Clients.builder();
// No need to define anything else; build the Client instance. The ClientCredential information will be automatically found
// in pre-defined locations: i.e. ~/.okta/okta.yaml
Client client = builder.build();
// Create a group
Group group = GroupBuilder.instance().setName("my-user-group-" + UUID.randomUUID().toString()).setDescription("Quickstart created Group").buildAndCreate(client);
println("Group: '" + group.getId() + "' was last updated on: " + group.getLastUpdated());
// Create a User Account
String email = "joe.coder+" + UUID.randomUUID().toString() + "@example.com";
char[] password = { 'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '1' };
User user = UserBuilder.instance().setEmail(email).setFirstName("Joe").setLastName("Coder").setPassword(password).setSecurityQuestion("Favorite security question?").setSecurityQuestionAnswer("None of them!").putProfileProperty("division", // key/value pairs predefined in the user profile schema
"Seven").setActive(true).buildAndCreate(client);
// add user to the newly created group
user.addToGroup(group.getId());
String userId = user.getId();
println("User created with ID: " + userId);
// You can look up user by ID
println("User lookup by ID: " + client.getUser(userId).getProfile().getLogin());
// or by Email
println("User lookup by Email: " + client.getUser(email).getProfile().getLogin());
// get the list of users
UserList users = client.listUsers();
// get the first user in the collection
println("First user in collection: " + users.iterator().next().getProfile().getEmail());
// or loop through all of them (paging is automatic)
// int ii = 0;
// for (User tmpUser : users) {
// println("["+ ii++ +"] User: " + tmpUser.getProfile().getEmail());
// }
} catch (ResourceException e) {
// we can get the user friendly message from the Exception
println(e.getMessage());
// and you can get the details too
e.getCauses().forEach(cause -> println("\t" + cause.getSummary()));
throw e;
}
}
use of com.okta.sdk.resource.ResourceException in project okta-sdk-java by okta.
the class DefaultDataStore method execute.
private Response execute(Request request) throws ResourceException {
applyDefaultRequestHeaders(request);
Response response = this.requestExecutor.executeRequest(request);
log.trace("Executed HTTP request.");
if (requestLog.isTraceEnabled()) {
requestLog.trace("Executing request: method: '{}', url: {}", request.getMethod(), request.getResourceUrl());
}
if (response.isError()) {
Map<String, Object> body = getBody(response);
String requestId = response.getHeaders().getOktaRequestId();
if (Strings.hasText(requestId)) {
body.put(DefaultError.ERROR_ID.getName(), requestId);
}
throw new ResourceException(new DefaultError(body).setHeaders(response.getHeaders().getXHeaders()).setStatus(response.getHttpStatus()));
}
return response;
}
Aggregations