【问题标题】:Authorization of Azure Storage service REST API for table表的 Azure 存储服务 REST API 的授权
【发布时间】:2018-05-02 15:22:23
【问题描述】:

我一整天都在调用 Azure 存储 REST API。响应显示这是由于 Azure 身份验证错误,但我不知道是什么问题。 你也可以在这里检查类似的问题

Authorization of Azure Storage service REST API

通过使用它,我得到了为我工作但不为桌子工作的血液。 如何用表格实现这一目标

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

sn-p 上面是访问 Azure 表存储。但是,这是行不通的。但是,如果我尝试对 blob 进行此操作,它似乎可以正常工作

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",

【问题讨论】:

  • 问题与链接中发布的内容类似,但针对“表”存储。
  • 请分享您的代码。
  • 正如 Gaurav 所说,请分享您的代码,以便其他人可以帮助您。另外,为什么不直接使用 Azure Storage Client Library 而不是自己编写认证代码呢?
  • 更新问题

标签: azure azure-storage azure-table-storage azure-tablequery


【解决方案1】:

strToSign 用于表身份验证与 Blob 不同。

您可以更改以下两个变量并尝试一下,它对我有用。

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

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

这里有一些参考资料供您参考。

【讨论】:

    【解决方案2】:

    我建议您使用以下教程进行身份验证,看看您是否遗漏了一步:https://www.youtube.com/watch?v=ujzrq8Fg9Gc
    这应该可以解决您的问题。

    这是 Azure 服务的 REST API 的链接:https://docs.microsoft.com/en-us/rest/api/azure/#register-your-client-application-with-azure-ad

    【讨论】:

      猜你喜欢
      • 2016-12-15
      • 1970-01-01
      • 2021-12-08
      • 2017-10-23
      • 2021-04-28
      • 1970-01-01
      • 2021-05-31
      • 2019-02-03
      • 1970-01-01
      相关资源
      最近更新 更多