Hi,
Greetings. I executed the sample program provided by Refinitiv world check id. The program was successful and working fine from my lab which is based in other location. I'm tried to execute the same program in my local environment, I'm always receiving the 401 error. I used same apikey and secret key.
I do not see any proxy issue as well with local system. is there anything to do with the timezone.
I tried to execute the sample request through postman. I received same error over postman as well. I'm not sure why the authorization failed with the same key and secret.
Please help me to resolve this issue. Please let me know if you want me to share the entire program.
Error Message :
15:31:03.025 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "https://rms-world-check-one-api-pilot.thomsonreuters.com/v1/reference/profile/e_tr_wci_4277049" resulted in 401 (null); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 null
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
Sample Program:
public static final String PROFILE_CHECK_API_ENDPOINT="https://rms-world-check-one-api-pilot.thomsonreuters.com/v1/reference/profile/e_tr_wci_4277049";
public static void main(String[] args) throws Exception {
getAuthorization();
}
public static void getAuthorization() throws KeyManagementException, NoSuchAlgorithmException, JsonParseException, JsonMappingException, IOException {
System.out.println("Inside Authorization Block!!!");
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 = "/v1/";
String gatewayhost = "rms-world-check-one-api-pilot.thomsonreuters.com";
String apikey = "";
String apisecret = "";
String dataToSign = "(request-target): get " + gatewayurl + "reference/profile/e_tr_wci_4277049" + "\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(" Authorization *** " + authorisation);
System.out.println("DATA TO SIGN " + dataToSign);
System.out.println(" HMAC " + hmac);
getApData(authorisation, date, PROFILE_CHECK_API_ENDPOINT);
}
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);
}
public static void getApData(String authorisation,String date,String url) throws JsonParseException, JsonMappingException, IOException{
RestTemplate rest = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", authorisation);
headers.set("Cache-Control", "no-cache");
headers.set("date", date);
HttpEntity<String>httpEntity = new HttpEntity<String>("",headers);
ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.GET,httpEntity, String.class);
int status = responseEntity.getStatusCodeValue();
System.out.println("status *** " + status);
String response = responseEntity.getBody();
ObjectMapper mapper = new ObjectMapper();
Object res = mapper.readValue(response, Object.class);
String intended = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(res);
System.out.println("response *** " + intended);
}