1
votes

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",
2
Issue is similar to what is posted in the link, but for "Table" storage.Vidhya Sagar Reddy
Please share your code.Gaurav Mantri
As Gaurav said, please share your code so others can help you. In addition, why not use Azure Storage Client Library directly rather than write your own authentication code?Zhaoxing Lu
Updated questionVidhya Sagar Reddy
@SandeepBhaskar Did my solution work for you? If so you could accept it as an answer. If you still have trouble in this, just ask.Jerry Liu

2 Answers

1
votes

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.

0
votes

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://docs.microsoft.com/en-us/rest/api/azure/#register-your-client-application-with-azure-ad