How to generate SharedKeyLite for Azure Table Storage REST request

时光怂恿深爱的人放手 提交于 2021-01-28 00:58:13

问题


I'm trying to call Azure Table Storage using Postman but keep getting :

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

The code I am using for the pre-call script in Postman is as follows:

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);

The headers in Postman are as follows:

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

I am guessing the issue may be with the data variable, but running out of ideas.


回答1:


The reason you're getting this error is because you're not converting your account key to a buffer. Please change the following line of code:

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

to

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

And you should not get the error.


UPDATE

I also got the same error. Please try the following code:

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);

I just tried the code above and was able to list entities in my table.



来源:https://stackoverflow.com/questions/56512864/how-to-generate-sharedkeylite-for-azure-table-storage-rest-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!