public class GetRequest {
public static String generateAuthHeader(String dataToSign, String secret) {
String hash = "";
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
hash = Base64.encodeBase64String(sha256_HMAC.doFinal(dataToSign.getBytes()));
} catch (Exception e) {
System.out.println("Error");
}
return (hash);
}
// A simple request to create a Case using Http Get
public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
Date now = new Date();
//format for date string Mon, 27 Mar 2017 15:19:36 GMT
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = df.format(now);
String gatewayurl = "/v2/";
String gatewayhost = "api-worldcheck.refinitiv.com";
String apikey = "XXXXXXXXX-XXXX-XXXX-XXXXX-e5ac69bef305";
String apisecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String dataToSign = "(request-target): get " + gatewayurl + "groups\n"
+ "host: " + gatewayhost + "\n"
+ "date: " + date;
String hmac = generateAuthHeader(dataToSign, apisecret);
String authorisation = "Signature keyId=\"" + apikey + "\",algorithm=\"hmac-sha256\",headers=\"(request-target) host date\",signature=\"" + hmac + "\"";
System.out.println("dataToSign " + dataToSign + "-----------------");
System.out.println("hmac " + hmac);
//System.out.println(authorisation);
HttpGet httpGet = new HttpGet("https://api-worldcheck.refinitiv.com/v2/groups");
httpGet.addHeader("Authorization", authorisation);
httpGet.addHeader("Cache-Control", "no-cache");
httpGet.addHeader("date", date);
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
HttpEntity entity1 = response1.getEntity();
System.out.println(response1.getStatusLine());
String json = EntityUtils.toString(response1.getEntity());
System.out.println("entity1 " +entity1);
System.out.println("json" + json);
ObjectMapper mapper = new ObjectMapper();
//StatusResponses loginValidator = null;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);
Object jsonObj = objectMapper.readValue(json, Object.class);
String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj);
System.out.println(indented);
EntityUtils.consume(entity1);
} finally {
response1.close();
}
} finally {
httpclient.close();
}
}
}