Azure tables rest api authorization

时间:2016-02-12 19:26:35

标签: azure azure-table-storage azure-powershell

I'm trying to use azure rest api from powershell but stuck with authorization part. Used to generate signature using this article https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx

Script:

$StorageAccount = "account"
$Key = "key"

$sharedKey = [System.Convert]::FromBase64String($Key)
$date = [System.DateTime]::UtcNow.ToString("R")

$resource = "/test()?`$top" # also tried /test() /test /test()?`$top=1
$stringToSign = "$date`n/$StorageAccount$resource"
$hasher = New-Object System.Security.Cryptography.HMACSHA256
$hasher.Key = $sharedKey

$signedSignature = [System.Convert]::ToBase64String($hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($stringToSign)))

$authHeader = "SharedKeyLite ${StorageAccount}:$signedSignature"

$headers = @{"x-ms-date"=$date
            "Authorization"=$authHeader
            "Accept"="application/atom+xml"}

try {
    $tables = Invoke-RestMethod -Uri "https://$StorageAccount.table.core.windows.net/test()?`$top=1" -Headers $headers |% {
        $_.content.properties.tablename
    }
} catch [Exception] {
    $_
}

I was able to list tables (/tables) but when I try to execute some odata requests (/test()?$top=1 here) I am getting authorization error.

1 个答案:

答案 0 :(得分:2)

我复制你的代码并在我的最后尝试。它工作正常。

以下是我想指出的一些事情。

  1. 对于"查询实体",您应使用$resource = "/test()"$resource = "/test"用于"插入实体"。 $resource = "/test()?$top"$resource = "/test()?$top=1"不正确。

  2. 确保您的$Key正确无误。由于您使用此密钥创建表格,我不会认为是这种情况。

  3. 确保表格中至少有一行。