Azure Table Storage (Table Services REST API) SharedKeyLite not working

Viewed 282

After seeing through this link, I tried the same in my postman.

var storageAccount = "mystorage";
var accountKey = "<<primaryKey>>";

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

var data = UTCstring + "\n" +  "/mystorage/Health"

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

When I make the request to ATS, to the following url, enter image description here

I get the auth denied! enter image description here

Can someone please guide me what's going wrong here?!

2 Answers

I think you need to generate a bearer token and put it to the Authorization of Postman.

If you are using C#, you can use this to get the bearer token:

AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
string accesstoken = azureServiceTokenProvider.GetAccessTokenAsync("https://storage.azure.com/").Result;
string bearertoken = "Bearer " + accesstoken;

Then Copy the bearer token:

enter image description here

After that, it should be ok.

Just realized that the url and the data that we are encoding should exactly match the url we are querying... After changing

var data = UTCstring + "\n" +  "/mystorage/Health"

to

var data = UTCstring + "\n" +  "/mystorage/Health(PartitionKey='USA',RowKey='WA')"

things started working.

Update : It just expects the right table query. The following works fine,

var data = UTCstring + "\n" +  "/mystorage/Health()" 

with all filter expressions in the url being invoked from postman.

Related