gpt4 book ai didi

javascript - Azure表存储通过javascript验证REST调用

转载 作者:行者123 更新时间:2023-11-27 23:21:00 25 4
gpt4 key购买 nike

我正在尝试使用 JavaScript 对我的 Azure 表进行 REST 调用,但我发现很难对调用进行身份验证。

我正在使用那段 javascript(我知道日期不得超过 15 分钟,而且我不打算使用 javascript 中的实际 key !)

$(document).ready(function(){
$("button").click(function(){
var dateTimeInUtc = 'Fri, 12 Feb 2016 12:14:00 GMT';
var version = '2015-04-05';
var key = 'JEwMjqFD1ng8vIaECmRw8eQysiIvH08nF/jPKPYaNGumgxtKIjltX8bte5sKN6SNyw09s=='; // not an actuall key
var stringToSign = 'GET\n\n\nFri, 12 Feb 2016 12:14:00 GMT\n/myaccount/mytable(PartitionKey=\'first_partition\', RowKey=\'1235\')';
var signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(CryptoJS.enc.Utf8.parse(stringToSign), CryptoJS.enc.Base64.parse(key)));
$.ajax({
url:'https://myaccount.table.core.windows.net/mytable(PartitionKey=\'first_partition\', RowKey=\'1235\')',
type: 'GET',
success: function (data) {
console.log('well done');
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "SharedKey " + "myaccount" + ":" + signature);
xhr.setRequestHeader('x-ms-date', dateTimeInUtc);
xhr.setRequestHeader('x-ms-version', version);
},
error: function (rcvData) {
console.log(rcvData);
}
});
});
});

我得到了什么

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

你发现有什么明显错误的地方吗?我是否遗漏了签名中的某些内容?

最佳答案

您构建的待签名字符串必须是通过线路传输的准确 URL,包括 URL 中不允许的百分比编码字符。在这种情况下,URL 中的空格字符将通过网络进行百分比编码,因此它必须在要签名的字符串中进行百分比编码。

来自MSDN page :

Any portion of the CanonicalizedResource string that is derived from the resource's URI should be encoded exactly as it is in the URI.

关于javascript - Azure表存储通过javascript验证REST调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com