我想在apple new publisher API上发表一篇文章。我正在尝试复制相同的python代码以在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)
这就是我签名的方式:
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);
}
我总是得到401:未经授权
答案 0 :(得分:0)
尝试以下方式:
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);