I understand there is no implementation for oAuth 1.0 inbuilt with akka so I used MasterCard Api which created OAuth String to be used as header. I used it with Java HttpsURLConnection client, OkHttp3 and it worked perfectly fine but when I use the same with Akka Http client I am getting status 500 error.
Okhttp returns status 200
public String fetchx(String url, String method, Map<String, String> headers, String body, String contentType) throws IOException,
UnrecoverableKeyException,
NoSuchAlgorithmException,CertificateException,KeyStoreException,IOException{
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.headers(Headers.of(headers))
.header("Authorization", createAuthHeader(URI.create(url), method, body, StandardCharsets.UTF_8))
.method(method, body != null ? RequestBody.create(MediaType.parse(contentType), body) : null)
.build();
System.out.println("OKHTTP api req: " + request.toString());
Call call = httpClient.newCall(request);
String response = null;
try {
Response res = call.execute();
response = res.body().string();
res.close();
System.out.println("api res: " + res.toString());
} catch (IOException e) {
response = e.getMessage();
}
return response;
}
Akka Http: returns 500
private CompletionStage<HttpResponse> fetch(String url,String method,String payload,Charset charset)throws UnrecoverableKeyException,
NoSuchAlgorithmException,CertificateException,KeyStoreException,IOException {
URI uri = URI.create(url);
String authHeader = createAuthHeader(uri, method, payload,charset);
HttpRequest req = HttpRequest.create(uri.toString())
.withMethod(HttpMethods.lookup(method).get())
.addHeader(HttpHeader.parse("Authorization", authHeader));
System.out.println("AKKA req: "+req.toString());
return http.singleRequest(
req
);
}
Most of my project uses Akka http client so it will be really painful to use another httpclient. Any advices will be highly appreciated.