0

我正在 Lua 中编写一些代码来在 Azure 表存储中创建一个项目。

我试过共享密钥签名:

local account = 'XXX'
local key = 'YYY'
local table = 'test'
local date = os.date('!%a, %d %b %Y %H:%M:%S GMT', os.time)

local sts = "POST\n" ..
            "\n" ..  --Content-MD5
            "application/json\n" ..  --Content-Type
            args.date .. "\n" ..  --Date
            string.format("/%s/%s", account, table)

和共享密钥精简版:

local sts = string.format("%s\n/%s/%s", date, account, table)

我也尝试过转义或不转义字符串:

local sts = ngx.escape_uri(sts)

然后我签名:

local signature = ngx.encode_base64(crypto.hmac.digest("sha256", sts, ngx.decode_base64(args.key), false))

然后发送请求:

local url = string.format("https://%s.table.core.windows.net/%s", account, table)
local auth = string.format('SharedKey %s:%s', account, signature)
-- or local auth = string.format('SharedKeyLite %s:%s', account, signature)
local item = cjson.encode(item)

local httpc = http.new()
local res, err = httpc:request_uri(url, {
            method = "POST",
            data = item,
            headers = {
                ["Authorization"] = auth,
                ["x-ms-date"] = date,
                ["Accept"] = "application/json;odata=nometadata",
                ["x-ms-version"] = "2015-12-11",
                ["Content-Type"] = "application/json",
                ["Content-Length"] = #item,
                ["DataServiceVersion"] = "3.0;NetFx"
            }
})

我收到此错误:

{"odata.error":{"code":"AuthenticationFailed","message":{"lang":"en-US","value":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:AAA\nTime:2017-02-16T17:51:19.6107765Z"}}}

为什么?谢谢你。

4

1 回答 1

0

要解决此问题,您可以尝试将可选的原始标志设置true为让crypto.hmac.digest()函数的输出成为直接二进制文件。

local signature = ngx.encode_base64(crypto.hmac.digest("sha256", sts, ngx.decode_base64(args.key), true))
于 2017-02-17T09:30:07.790 回答