【问题标题】:Signature for Azure Table Storage REST APIAzure 表存储 REST API 的签名
【发布时间】:2017-02-16 17:58:51
【问题描述】:

我正在 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"}}}

为什么?谢谢。

【问题讨论】:

    标签: azure lua azure-table-storage openresty


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-06
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2021-04-28
      • 2018-12-07
      相关资源
      最近更新 更多