如何为Azure表存储REST请求生成SharedKeyLite [英] How to generate SharedKeyLite for Azure Table Storage REST request

查看:39
本文介绍了如何为Azure表存储REST请求生成SharedKeyLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Postman调用Azure Table Storage,但一直保持:

I'm trying to call Azure Table Storage using Postman but keep getting :

服务器无法验证请求.确保的价值授权标头的格式正确,包括签名.

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

我在Postman中用于预呼叫脚本的代码如下:

The code I am using for the pre-call script in Postman is as follows:

var storageAccount = "**mystorageaccount**";
var accountKey = "**mystoragekey**";

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

var data = date + "\n" + "/**mystorageaccount**/**mytable**"

var encodedData = unescape(encodeURIComponent(data));

var hash = CryptoJS.HmacSHA256(encodedData, accountKey);
var signature = hash.toString(CryptoJS.enc.Base64);

var auth = "SharedKeyLite " + storageAccount + ":" + signature;

postman.setEnvironmentVariable("auth", auth);
postman.setEnvironmentVariable("date", UTCstring);

Postman中的标题如下:

The headers in Postman are as follows:

Authorization : {{auth}}
date : {{date}}
version : 2015-12-11

我猜想问题可能出在数据变量上,但想法不多了.

I am guessing the issue may be with the data variable, but running out of ideas.

推荐答案

出现此错误的原因是,您没有将帐户密钥转换为缓冲区.请更改以下代码行:

The reason you're getting this error is because you're not converting your account key to a buffer. Please change the following line of code:

var hash = CryptoJS.HmacSHA256(encodedData, accountKey);

var hash = CryptoJS.HmacSHA256(encodedData, Buffer.from(accountKey, 'base64'));

而且您不应收到错误消息.

And you should not get the error.

更新

我也遇到了同样的错误.请尝试以下代码:

I also got the same error. Please try the following code:

var storageAccount = "**mystorageaccount**";
var accountKey = "**mystoragekey**";

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

var data = UTCstring + "\n" + "/**mystorageaccount**/**mytable**"

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

我刚刚尝试了上面的代码,并能够列出表中的实体.

I just tried the code above and was able to list entities in my table.

这篇关于如何为Azure表存储REST请求生成SharedKeyLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆