I want to post an article on the apple new publisher API. I am trying to replicate the same python code to create an article in the apple documentation
String body = new ObjectMapper().writeValueAsString(articleModel); //a json model that represents the article
String finalUrl = baseURL + channelId + "/articles/";
String date= getCurrentDate();
String canonical_request = "POST"+finalUrl + date + "multipart/form-data; boundary=1906ef19a2044180b914d742c37e2ace"+ body;
String authHeader = "HHMAC; key="+apiKeyId+";signature="+getSignature(secret, canonical_request)+";date="+date;
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(finalUrl);
StringEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(body));
httpPost.setEntity(entity);
httpPost.setHeader("Content-type", "'multipart/form-data; boundary=1906ef19a2044180b914d742c37e2ace'");
httpPost.setHeader("Authorization", authHeader);
CloseableHttpResponse response = client.execute(httpPost)
This is how I am getting my signature:
private static String getSignature(String key, String data) {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(Base64.getDecoder().decode(key), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte [] m = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
return Base64.getEncoder().encodeToString(m);
}
I am always getting a 401: Unauthorized
Try the following way: