【发布时间】:2020-10-10 01:07:03
【问题描述】:
我想用 Postman 测试 Azure 存储服务的 API。为此,我需要一个之前必须编码的共享密钥。 我的问题是,当我尝试 GET-Request 时,我收到一条错误消息 我做了官方微软文档中的步骤:https://docs.microsoft.com/de-de/rest/api/storageservices/authorize-with-shared-key#constructing-the-canonicalized-headers-string
这是我的编码过程代码:
public static String account ="ACCOUNT NAME";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE , dd MMM yyyy HH:mm:ss O");
String date = formatter.format(ZonedDateTime.now(ZoneOffset.UTC));
String stringToSign = "GET\n"
+ "\n" // content encoding
+ "\n" // content language
+ "\n" // content length
+ "\n" // content md5
+ "\n" // content type
+ date +"\n" // date
+ "\n" // if modified since
+ "\n" // if match
+ "\n" // if none match
+ "\n" // if unmodified since
+ "\n" // range
+ "x-ms-date:" + date
+ "\nx-ms-version:2019-07-07\n"
+ "/" + account + "/"
+"\ncomp:list"; // resources
public DirectoryController() throws Exception {
try {
String auth = getAuthenticationString(stringToSign);
System.out.println(auth + date);
}catch (Exception ex) {
throw new Exception();
}
}
private static String getAuthenticationString(String stringToSign) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
String key = "KEY";
mac.init(new SecretKeySpec(Base64.decode(key), "HmacSHA256"));
String authKey = new String(Base64.encode(mac.doFinal(stringToSign.getBytes("UTF-8"))));
String auth = "SharedKey " + account + ":" + authKey;
return auth;
}
GET 请求; https://ACCOTUNAME.file.core.windows.net/?comp=list
我是否在编码过程中设置了错误。 我发现日期格式不像微软预期的那样。我得到神父。作为一周中的一天,微软想要星期五。我怎么得到这个?
【问题讨论】:
标签: azure azure-storage