azure - Azure 存储 API - 使用共享密钥的授权如何工作?
问题描述
我想用 Postman 测试 Azure Storage Service 的 API。为此,我需要一个之前必须编码的共享密钥。我的问题是,当我尝试 GET-Request 时收到一条错误消息,我执行了官方 microsoft 文档中的步骤:https ://docs.microsoft.com/de-de/rest/api/storageservices/authorize-with-共享密钥#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;
}
获取请求;https://ACCOTUNAME.file.core.windows.net/?comp=list
我是否在编码过程中设置了错误。我发现日期格式不像微软预期的那样。我得到神父。作为星期几和微软想要星期五..我怎么得到这个?
解决方案
好的,我们可以关闭这个线程。我解决了这个问题。这是错误的日期格式。使用 Local.US 并且在 //date 前面没有日期参数,它可以工作。
推荐阅读
- javascript - 我们可以在 sql 中将对象作为参数传递给数组,以防止使用 promise-mysql 模块在 nodejs 中注入 sql
- azure-devops - 自定义 Azure DevOps 项目的首页
- python - 为什么 numpy.int16 在我位掩码后变成 numpy.float64?
- python - 时间序列数据的 sqlalchemy 查询,格式为 (step, next_step) 时间相邻样本对
- java - 如何使用 Java 读取 XML 响应
- c# - 如何在c#中比较2个数据表
- php - PHP通过给定键获取值
- r - Zelig (R) 中的混合 Logit 模型 - 未运行 - 不再可用?
- unit-testing - 是否可以为保存/更新/持久方法编写测试用例 - 无论是模拟还是通过调用真实方法
- sql - 使用 SQL 计算连续重复字段