Reputation: 149
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.
Upvotes: 1
Views: 659
Reputation: 2267
I copy your code and try it at my end. It works fine.
Here are somethings I want to point out.
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.
Make sure your $Key
is correct. Since you have use this key for creating the table, I don't think this is the case.
Make sure there is at least one row in your table.
Upvotes: 2