我想通过postman调用azure文件存储相关的REST API。 以下是我提出请求的方式:
我正在请求列出文件存储帐户中的所有共享,如下所述: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:
当我提供正确格式的日期时,出现如下错误:
我收到以下错误: “在 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中重新发送请求。
重要通知:
上面的代码中,//resources行中不要漏掉“\ncomp:list”,否则也会返回403错误。 您可以在 Constructing the Canonicalized Resource String 中找到规则。 .
关于java - 如何使用postman调用REST API进行azure文件存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45158655/