Azure 表格存储 REST API 的签名

我正在使用 Lua 编写一些代码,以创建 Azure 表格存储中的项目。

我已经尝试了 Shared Key 签名方法:

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
" ..
            "
" ..  - Content-MD5
            "application/json
" ..  - Content-Type
            args.date .. "
" ..  - Date
            string.format("/%s /%s",account,table)

和 Shared Key Lite 签名方法:

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,tablelocal auth = string.format'SharedKey%s:%s',account,signature)
-- 或 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"}}}

为什么?谢谢。

点赞
用户5341577
用户5341577

为了解决这个问题,你可以尝试将可选的_raw_标志设置为true,让crypto.hmac.digest()函数的输出成为直接二进制格式。

local signature = ngx.encode_base64(crypto.hmac.digest("sha256", sts, ngx.decode_base64(args.key), true))
2017-02-17 09:30:07