我想使用苹果新发布者 API 访问我的频道。我正在尝试在苹果文档中复制相同的 python
String finalUrl = baseURL + channelId;
String date= getCurrentDate();
String canonical_request = "GET"+finalUrl + date;
String authHeader = "HHMAC;key="+apiKeyId+";signature="+getSignature(secret, canonical_request)+";date="+date;
CloseableHttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.get()
.setUri(finalUrl)
.setHeader(HttpHeaders.AUTHORIZATION, authHeader)
.build();
CloseableHttpResponse response = client.execute(request);
我如何获得签名:
private static String getSignature(String secret, String data) {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
return Base64.encodeBase64String(sha256_HMAC.doFinal(data.getBytes()));
}
我总是收到 401。python 代码正在运行。我试图从 python 代码中复制错误,它仅在我未设置授权标头时发生,而不是在签名错误时发生(也根据苹果新闻发布者 API文档),这意味着标头未设置。您可以在此处查看整个请求