2016-02-12 157 views
0

我想从powershell中使用azure休息API,但坚持授权部分。用于使用本文https://msdn.microsoft.com/en-us/library/azure/dd179428.aspxAzure表休息api授权

脚本生成签名:

$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] { 
    $_ 
} 

我能列出表(/表),但是当我试图执行一些OData的请求(/测试()$ top = 1 here)我收到授权错误。

回答

2

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

这里有一些我想指出的。

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

  2. 确保您的$Key是正确的。既然你已经使用这个键来创建表,我不认为是这种情况。

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

+0

感谢您的回复! '$ Key'是我的存储帐户中的主键。当我将$ resource设置为'/ test()'请求失败并出现auth错误时'服务器无法验证请求。确保授权标头的值正确形成,包括签名.' – forik

+0

我还使用'$ key'创建了新表,但我继续收到授权错误:( – forik

+0

)您的脚本是正确的。尝试使用Azure存储资源管理器存储帐户和密钥如果不工作,请重新生成密钥。 –