Publishing with Apple news format

311 views Asked by At

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

1

There are 1 answers

0
taz On

Try the following way:

private byte[] decodedKey = Base64.getDecoder().decode(api_key_secret);
private HmacUtils hmacUtils = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, decodedKey);

byte[] base64EncodedMac = Base64.getEncoder().encode(hmacUtils.hmac(outputStream.toByteArray()));

String signature = new String(base64EncodedMac);
String authorization = String.format("HHMAC; key=%s; signature=%s; date=%s", api_key_id, signature, date);