Skip to content

Instantly share code, notes, and snippets.

@pmedcraft
Last active April 15, 2019 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmedcraft/5a8ee5dce45bb814e1b3a551eadbdf3f to your computer and use it in GitHub Desktop.
Save pmedcraft/5a8ee5dce45bb814e1b3a551eadbdf3f to your computer and use it in GitHub Desktop.
Authenticates a user against SDL WorldServer using the REST API returning a Security Token
public static String getSecurityToken(String wsBaseUrl, String username, String password) throws IOException {
StringBuilder loginJson = new StringBuilder();
loginJson.append("{");
loginJson.append("\"username\":\"" + username + "\",");
loginJson.append("\"password\":\"" + password + "\"");
loginJson.append("}");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPostRequest = new HttpPost(wsBaseUrl + "/ws-api/v1/login");
httpPostRequest.setHeader(new BasicHeader("Content-Type", "application/json"));
httpPostRequest.setHeader(new BasicHeader("Accept", "application/json"));
httpPostRequest.setEntity(new StringEntity(loginJson.toString()));
HttpResponse response = httpClient.execute(httpPostRequest);
Gson gson = new GsonBuilder().create();
JsonObject jsonLoginResponse = gson
.fromJson(new InputStreamReader(response.getEntity().getContent()), JsonObject.class);
return jsonLoginResponse.getAsJsonPrimitive("sessionId").getAsString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment