首页 > 解决方案 > 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

我是否在编码过程中设置了错误。我发现日期格式不像微软预期的那样。我得到神父。作为星期几和微软想要星期五..我怎么得到这个?

标签: azureazure-storage

解决方案


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


推荐阅读