java - 如何使用postman调用REST API进行azure文件存储?

标签 java rest azure logic postman

我想通过postman调用azure文件存储相关的REST API。 以下是我提出请求的方式:

enter image description here

我正在请求列出文件存储帐户中的所有共享,如下所述:https://learn.microsoft.com/en-us/rest/api/storageservices/list-shares

我收到以下错误:

"The Date header in the request is incorrect." What changes I should make ?

编辑1:

当我提供正确格式的日期时,出现如下错误:

enter image description here

我收到以下错误: “在 HTTP 请求 '' 中找到的 MAC 签名与任何计算出的签名不同。服务器使用以下字符串进行签名:'GET”

如何解决这个问题?

最佳答案

根据您更新的屏幕截图,您的问题似乎不再与 x-ms-date 有关。 出现此 403 错误的原因是 header 中的 Authorization 属性,其格式为

Authorization="[SharedKey|SharedKeyLite] [AccountName]:[Signature]"

您不应该直接使用Azure Portal上的访问 key 作为授权签名部分,而是应该构建它 使用 HMAC-SHA256 算法对 UTF-8 编码的请求字符串进行编码。 格式为

Signature=Base64(HMAC-SHA256(UTF8(StringToSign)))

其中提到official document .

下面的示例java代码向您展示了如何构建授权的签名部分:

String stringToSign = "GET\n" 
       + "\n" // content encoding
       + "\n" // content language
       + "\n" // content length
       + "\n" // content md5
       + "\n" // content type
       + "\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:2015-02-21\n" // headers
       + "/" + <your account name> + "/"+"\ncomp:list"; // resources
       String auth = getAuthenticationString(stringToSign);

private static String getAuthenticationString(String stringToSign) throws Exception {
       Mac mac = Mac.getInstance("HmacSHA256");
       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;
}

代码中的auth参数是由您上面提到的Signature生成的,然后您可以将其填写到Authorization属性中并在Postman中重新发送请求。

截图如下: enter image description here

重要通知:

上面的代码中,//resources行中不要漏掉“\ncomp:list”,否则也会返回403错误。 您可以在 Constructing the Canonicalized Resource String 中找到规则。 .

关于java - 如何使用postman调用REST API进行azure文件存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45158655/

相关文章:

rest - Golang 中的动态类型转换

c# - 具有不同 Controller 但相同操作名称的路由无法生成所需的 url

c# - .NET 5.0 独立 Azure 函数未触发代码 - 超时

Java:在 Windows 计算机上打印/输出彩色和格式化文本到控制台

java - 无法理解 SQL 异常 - Ora 错误代码 :ORA-01000:

java - 继承方法返回引用类型

java - 使用 S3 路径创建获取休息请求以进行事务

powershell - Azure Function - PowerShell,安装 az cmdlet::术语 'az' 未被识别为 cmdlet 的名称

azure - 在 Azure 中将 Kudu 与多实例应用服务结合使用

java - 在 Hibernate 中使用 load 而不是 get 时遇到 LazyInitializationException