使用 Azure Table SAS 令牌读取数据使用 Invoke-RestMethod

Use Azure Table SAS token to read data using Invoke-RestMethod

我想知道是否可以在使用 PowerShell 对 Azure 表的 REST API 调用中使用 SAS 令牌进行授权 header。我创建了一个测试帐户 SAS,并尝试传递以 "sr=" 标记开头的实际令牌值以及完整的 URI,但是我收到以下错误:

Invoke-RestMethod : AuthenticationFailed Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

$resource = "$tableName(PartitionKey='$domain',RowKey='$apiKey')"
$tableUrl = "https://$storageAccount.table.core.windows.net/$resource"

$sasReadToken = "SharedAccessSignature ?sv=2017-07-29&ss=t&srt=o&sp=r&se=2019-03-07T02:37:08Z&st=2018-03-06T18:37:08Z&spr=https&sig=<removed>"

$GMTTime = (Get-Date).ToUniversalTime().toString('R')

$header = @{
    Authorization = $sasReadToken;
    'x-ms-date'    = $GMTTime;
    Accept         = "application/json;odata=fullmetadata";
}

$result = Invoke-RestMethod -Uri $tableUrl -Headers $header -Method Get -Verbose

虽然我知道有一个 AzureRm 模块可以处理其中的一些问题,但我不想在主机 PC 上安装不必要的库。这可能吗?

注意:签名在我的示例中已被删除。

SAS 令牌在授权中无效 header。它们仅作为 collection 个查询字符串参数有效。

有关 Azure 存储 SAS 令牌的详细信息,请参阅 https://docs.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1

我能够使用我的 SAS 作为 URI 的一部分,使用以下代码对存储进行身份验证。 请注意,SAS 令牌是 $tableUri 变量的一部分,而不是 header 的一部分。我还必须在 header 中添加 Accept = 'application/json;odata=nometadata' 参数,否则我会收到 (415) 错误(不支持的媒体类型)。最后,我不得不在 PowerShell 中添加 -UseBasicParsing 来读取返回的数据。

function Get-MyAdvisorToken {
[cmdletbinding()]
param (
    [parameter()]$MyAdvisorApiKey,
    [parameter()]$DomainName
)

#retrieves SaSToken from Azure Table when supplying the API KEY and DOMAIN
$partitionKey = $DomainName #partitionKey
$rowKey = $MyAdvisorApiKey #rowKey
$sasReadToken = "?sv=2017-07-29&ss=t&srt=o&sp=r&se=2018-03-06T19:37:08Z&st=2018-03-06T18:37:08Z&spr=https&sig=<removed>"
$tableUri = "https://$storageAccount.table.core.windows.net/$tableName(PartitionKey='$partitionKey',RowKey='$rowKey')$sasReadToken"

$GMTTime = (Get-Date).ToUniversalTime().toString('R')
$header = @{
    'x-ms-date'    = $GMTTime;
    Accept = 'application/json;odata=nometadata'
}

$result = Invoke-WebRequest -Uri $tableUri -Headers $header -UseBasicParsing
$jsonResult = $result.Content | ConvertFrom-Json
return $jsonResult
}