Sandeep Bhaskar
Sandeep Bhaskar

Reputation: 300

Authorization of Azure Storage service REST API for table

i've been stuck for the whole day making my call of Azure Storage REST API. response showed that it's due to error in Azure authentication, but I have no idea what's the problem. you can check the similar problem here also

Authorization of Azure Storage service REST API

by using this i got the blod worked for me but not for the table. how to achieve this with the table

var apiVersion = '2017-07-29';
var storageAccountName = "MyAccountName";
var key = "MyAccountKey";

var currentDate= new Date();
var strTime = currentDate.toUTCString();
var strToSign = 'GET\n\n\n\nx-ms-date:' + strTime + '\nx-ms-version:' + apiVersion + '\n/' + storageAccountName + '/?restype=service&comp=properties';
var secret = CryptoJS.enc.Base64.parse(key);
var hash = CryptoJS.HmacSHA256(strToSign, secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKeyLite " + storageAccountName + ":" + hashInBase64; 
document.write(auth)
$.ajax({
    type: "GET",
    url: "https://MyAccountName.table.core.windows.net/?restype=service&comp=properties&sv=2017-07-29&ss=bqtf&srt=sco&sp=rwdlacup",
    beforeSend: function (request) {
        request.setRequestHeader("Authorization", auth);
        request.setRequestHeader("x-ms-date", strTime);
        request.setRequestHeader("x-ms-version", apiVersion);
    },
    processData: false,
    success: function (msg) {
        // Do something
    },
    error: function (xhr, status, error) {
        // Handle error
    }
});

Above snippet is to access Azure table storage. However this is not working. But if I try this against blob, it seems to be working

var strToSign = 'GET\n\n\n\nx-ms-date:' + strTime + '\nx-ms-version:' + apiVersion + '\n/' + storageAccountName + '/?comp=list';
url: "https://appcrestdev.blob.core.windows.net/?comp=list",

Upvotes: 1

Views: 717

Answers (2)

Jerry Liu
Jerry Liu

Reputation: 17790

strToSign for table authentication is different from that for blob.

You can change two variables as below and have a try, it works on my side.

var strToSign = strTime + '\n/' + storageAccountName + '/?comp=properties';

url: "https://MyAccountName.table.core.windows.net/?restype=service&comp=properties"

And here's some references for you.

Upvotes: 1

I'd recommend using the following tutorial for authentication to see if you've missed a step: https://www.youtube.com/watch?v=ujzrq8Fg9Gc
This should resolve your issues.

This is the link to the REST API for Azure services: https://learn.microsoft.com/en-us/rest/api/azure/#register-your-client-application-with-azure-ad

Upvotes: 0

Related Questions