【问题标题】:How to call REST API for azure file storage using postman?如何使用邮递员调用 REST API 进行 Azure 文件存储?
【发布时间】:2017-07-19 17:55:52
【问题描述】:

我想通过邮递员调用与azure文件存储相关的REST API。 这是我提出要求的方式:

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

我遇到以下错误:

“请求中的日期标头不正确。” 我应该做些什么改变?

编辑1:

当我提供正确格式的日期时,我有这样的错误:

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

如何解决?

【问题讨论】:

  • 您需要计算授权标头值。这里有很好的记录:docs.microsoft.com/en-us/rest/api/storageservices/….
  • 请查看更新后的问题..
  • 您能否深入了解一下您是如何构建Signature 的?它看起来比示例中的要长。
  • 您好,密钥与我的 Azure 门户帐户的“访问密钥”选项卡中提供的密钥相同。你有什么建议?
  • 您应该计算签名,而不仅仅是使用访问密钥。查看更新的答案。

标签: java rest azure logic postman


【解决方案1】:

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

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

您不应该直接使用 Azure Portal 上的 Access Key 作为 AuthorizationSignature 部分,而应该构造它 使用 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 中找到规则。

【讨论】:

【解决方案2】:

您指向的链接包含此示例:

PUT http://myaccount/mycontainer?restype=container&timeout=30 HTTP/1.1  
x-ms-version: 2015-02-21  
x-ms-date: Fri, 26 Jun 2015 23:39:12 GMT  
Authorization: SharedKey myaccount:ctzMq410TV3wS7upTBcunJTDLEJwMAZuFPfr0mrrA08=  
Content-Length: 0  

您使用的日期格式不正确:

所有经过身份验证的请求都必须包含请求的协调世界时 (UTC) 时间戳。您可以在 x-ms-date 标头或标准 HTTP/HTTPS 日期标头中指定时间戳。如果请求中同时指定了这两个头,则使用 x-ms-date 的值作为请求的创建时间。

编辑:
在授权标头上,您链接中文章中的Authentication for the Azure Storage Services 链接声明如下:

要对请求进行身份验证,您必须使用发出请求的帐户的密钥对请求进行签名,并将该签名作为请求的一部分传递。

Authorization 标头的格式如下:
授权="[SharedKey|SharedKeyLite] :"
其中SharedKeySharedKeyLite 是授权方案的名称,AccountName 是请求资源的帐户的名称,Signature 是从请求构造并计算得出的基于哈希的消息身份验证代码 (HMAC)使用SHA256算法,然后使用Base64编码。

这里有更多关于Hash-based Message Authentication Code (HMAC)的信息

自从您发表评论后:

您好,密钥与我的 Azure 门户帐户的“访问密钥”选项卡中提供的密钥相同。你有什么建议?

这就是问题所在。您不应该将密钥用作消息的Signature,您需要计算 Signature 基于密钥。

简而言之:

  • 使用“访问密钥”选项卡中的密钥计算请求的 SHA256 哈希
  • Base64 编码 SHA256 哈希输出
  • 使用 Base64 编码的字符串作为Signature

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-12-07
  • 2017-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 2017-05-19
  • 1970-01-01
相关资源
最近更新 更多