【问题标题】:Azure Storage API - How authorization with shared key is working?Azure 存储 API - 使用共享密钥的授权如何工作?
【发布时间】: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


    【解决方案1】:

    好的,我们可以关闭这个帖子了。我解决了这个问题。这是错误的日期格式。使用 Local.US 并且在 //date 前面没有日期参数,它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2020-12-21
      • 2015-04-26
      • 1970-01-01
      • 2013-06-19
      • 2021-10-30
      • 2018-03-20
      相关资源
      最近更新 更多