1
votes

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

1 Answers

2
votes

I copy your code and try it at my end. It works fine.

Here are somethings I want to point out.

  1. For "Query Entities", you should use $resource = "/test()", and $resource = "/test" is for "Insert Entity". $resource = "/test()?$top" and $resource = "/test()?$top=1" are not correct.

  2. Make sure your $Key is correct. Since you have use this key for creating the table, I don't think this is the case.

  3. Make sure there is at least one row in your table.