运行 针对 Azure 存储的查询 table returns 403 AuthenticationFailed,但返回 table returns 200 OK 中的所有条目

Running a query against an Azure storage table returns 403 AuthenticationFailed, but returning all entries in a table returns 200 OK

我无法使用 this documentation.

查询存储 tables

这是我用来获取共享密钥以便针对 Azure 存储进行身份验证的函数 table。

function Get-SharedKeyLiteAuthHeader {
    param(
        [Parameter(Mandatory = $TRUE)]
        [String]
        $StorageAccount,
        [Parameter(Mandatory = $TRUE)]
        [String]
        $TableName,
        [Parameter(Mandatory = $TRUE)]
        [String]
        $AccessKey,
        [Parameter(Mandatory = $FALSE)]
        [String]
        $Version = "2020-04-08"
    )
    $GMTTime = (Get-Date).ToUniversalTime().toString('R')
    $StringToSign = "$GMTTime`n/$($StorageAccount)/$($TableName)"
    $Hmacsha = New-Object System.Security.Cryptography.HMACSHA256
    $Hmacsha.key = [Convert]::FromBase64String($AccessKey)
    $Signature = $Hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($StringToSign))
    $Signature = [Convert]::ToBase64String($Signature)

    return @{
        'x-ms-date'    = $GMTTime
        Authorization  = "SharedKeyLite " + $StorageAccount + ":" + $Signature
        "x-ms-version" = $Version
        Accept         = "application/json;odata=fullmetadata"
    }
}

这是我正在进行的 REST 调用,returns 所有 table 条目和 returns 状态代码 200。

$Uri = "https://$($StorageAccount).table.core.windows.net/$($TableName)"
$Headers = Get-SharedKeyLiteAuthHeader -StorageAccount $StorageAccount -TableName $TableName -AccessKey $AccessKey

$AllEntries = Invoke-RestMethod -Method GET -Uri $Uri -Headers $Headers -ContentType application/json

这是我正在进行的 REST 调用 returns 403 AuthenticationFailed。

$Uri = "https://$($StorageAccount).table.core.windows.net/$($TableName)()?$top=2"
$Headers = Get-SharedKeyLiteAuthHeader -StorageAccount $StorageAccount -TableName $TableName -AccessKey $AccessKey

$SomeEntries = Invoke-RestMethod -Method GET -Uri $Uri -Headers $Headers -ContentType application/json

我的最终目标是 filter by date, but I can't get any of the query parameters working. I suspect it has to do with missing header elements, but I can't pin down what those might be as this documentation 讨论所需的 header 元素列出我已经指定的所有元素。

感谢任何帮助 - 谢谢。

尝试更改URL,只需删除()

https://$($StorageAccount).table.core.windows.net/$($TableName)?$top=2


我试过你的代码,它 return AuthenticationFailed.

Invoke-RestMethod : {"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.

也许你会在得到这个错误时改变StringToSign。但是,它是正确的,不需要添加查询字符串,请参阅 here

The query string should include the question mark and the comp parameter (for example, ?comp=metadata). No other parameters should be included on the query string.