I have tried to implement MLE with spring REST template and the api call seems getting some authentication error this is sample code I used
HttpHeaders newHeaders = new HttpHeaders() {{
String auth = visaBasicAuthUsername + ":" + visaBasicAuthPassword;
Charset charset = StandardCharsets.UTF_8;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(charset));
String authHeader = "Basic " + new String(encodedAuth, charset);
set("Authorization", authHeader);
}};
//newHeaders.setBasicAuth(visaBasicAuthUsername, visaBasicAuthPassword);
newHeaders.setContentType(MediaType.APPLICATION_JSON);
newHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
newHeaders.set("keyId", keyId);
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.sslContext(sslcontext)
.connectTimeout(Duration.ofSeconds(10))
.build();
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
String plainText = visaForeignExchangeLookUpRequest == null ? "" : mapper.writeValueAsString(visaForeignExchangeLookUpRequest);
JWEHeader.Builder headerBuilder = new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP_256, EncryptionMethod.A128GCM);
headerBuilder.keyID(keyId);
headerBuilder.type(JOSEObjectType.JOSE);
headerBuilder.customParam("iat", System.currentTimeMillis());
JWEObject jweObject = new JWEObject(headerBuilder.build(), new Payload(plainText));
RSAPublicKey publicKey = VisaCertificateUtils.loadPublicKeyFromFile(publicCertificatePath);
jweObject.encrypt(new RSAEncrypter(getRSAPublicKey(publicCertificatePath)));
HttpEntity<?> body = new HttpEntity<>(VisaRequestEncryptor.getEncryptedPayload(visaForeignExchangeLookUpRequest, keyId, mleServerPublicCertificatePath), newHeaders);
ResponseEntity<String> encryptedResponseStr = restTemplate.postForEntity("https://sandbox.api.visa.com/forexrates/v2/foreignexchangerates", body, String.class);
the response message is
Hi @hgebreselassie! Thank you for reaching out. One of our agents will look into this and get back to you soon. Until then, if any community members know a solution, please feel free to reply to this thread.
Hi @hgebreselassie,
To troubleshoot and fix the error, please refer to this community forum post here.