如何为 Azure Table 存储 REST 请求生成 SharedKeyLite

How to generate SharedKeyLite for Azure Table Storage REST request

我正在尝试使用 Postman 调用 Azure Table 存储,但不断收到:

Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

我在 Postman 中用于预调用脚本的代码如下:

var storageAccount = "**mystorageaccount**";
var accountKey = "**mystoragekey**";

var date = new Date();
var UTCstring = date.toUTCString();

var data = date + "\n" + "/**mystorageaccount**/**mytable**"

var encodedData = unescape(encodeURIComponent(data));

var hash = CryptoJS.HmacSHA256(encodedData, accountKey);
var signature = hash.toString(CryptoJS.enc.Base64);

var auth = "SharedKeyLite " + storageAccount + ":" + signature;

postman.setEnvironmentVariable("auth", auth);
postman.setEnvironmentVariable("date", UTCstring);

Postman中的headers如下:

Authorization : {{auth}}
date : {{date}}
version : 2015-12-11

我猜问题可能出在数据变量上,但 运行 没有想法。

您收到此错误的原因是您没有将帐户密钥转换为缓冲区。请更改以下代码行:

var hash = CryptoJS.HmacSHA256(encodedData, accountKey);

var hash = CryptoJS.HmacSHA256(encodedData, Buffer.from(accountKey, 'base64'));

而且你不应该得到这个错误。


更新

我也遇到了同样的错误。请尝试以下代码:

var storageAccount = "**mystorageaccount**";
var accountKey = "**mystoragekey**";

var date = new Date();
var UTCstring = date.toUTCString();

var data = UTCstring + "\n" + "/**mystorageaccount**/**mytable**"

var encodedData = unescape(encodeURIComponent(data));

var hash = CryptoJS.HmacSHA256(encodedData, CryptoJS.enc.Base64.parse(accountKey));
var signature = hash.toString(CryptoJS.enc.Base64);

var auth = "SharedKeyLite " + storageAccount + ":" + signature;

postman.setEnvironmentVariable("auth", auth);
postman.setEnvironmentVariable("date", UTCstring);

我刚刚尝试了上面的代码,并且能够在我的 table 中列出实体。