【问题标题】:Azure Batch REST API, authorization issueAzure Batch REST API,授权问题
【发布时间】:2019-11-24 21:13:14
【问题描述】:

我尝试使用 REST API 对 Azure Batch 进行身份验证,为此我编写了以下 PowerShell 代码

$Key = 'key'
$region = "region"
$sharedKey = [System.Convert]::FromBase64String($Key)
$date = [System.DateTime]::UtcNow.ToString("R")
$stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`nocp-date:$date`n /$batchAccount/jobs`napi-version:2019-08-01.10.0`ntimeout:20"
[byte[]]$dataBytes = ([System.Text.Encoding]::UTF8).GetBytes($stringToSign)
$hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha256.Key = [Convert]::FromBase64String($key)
$sig = [Convert]::ToBase64String($hmacsha256.ComputeHash($dataBytes))
$authhdr = "SharedKey $BatchAccount`:$sig"
$headers = @{
    "ocp-date" = $date;
    "Authorization" = "$authhdr";
}
Invoke-restmethod -Headers $headers -Uri 'https://$BatchAccount.$region.batch.azure.com/jobs?api-version=2019-08-01.10.0'

请注意,我知道我可以

  • 使用 OAuth2 作为替代身份验证机制
  • 使用 Az.Batch powershell 模块

我只是想使用此处描述的 REST 和 SharedKey 方案来完成此操作

https://docs.microsoft.com/en-us/rest/api/batchservice/authenticate-requests-to-the-azure-batch-service

对于这个 API

https://docs.microsoft.com/en-us/rest/api/batchservice/job/list

但由于某种原因它不起作用

我收到此错误,但一切似乎都遵循文档

"message":{
    "lang":"en-US",
    "value":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:eb5134f2-2821-4244-ac5b-066bf19bec10\nTime:2019-11-24T21:08:20.3223384Z"
},
"values":[
    {
        "key":"AuthenticationErrorDetail",
        "value":"The MAC signature found in the HTTP request 'signature-goes-here' is not the same as any computed signature. Server used following string to sign: 'GET\n\n\n\n\napplication/json; odata=minimalmetadata; charset=utf-8\n\n\n\n\n\n\nocp-date:Sun, 24 Nov 2019 21:08:20 GMT\n/name-goes-here/jobs\napi-version:2019-08-01.10.0'."
    }
]

【问题讨论】:

    标签: azure powershell rest azure-batch


    【解决方案1】:

    $stringToSign 有问题。试试这个:

    $Key = "your key"
    $region = "your region"
    $BatchAccount = "your account name"
    $BatchAccountURL = "Https://$BatchAccount.$region.batch.azure.com"
    
    $sharedKey = [System.Convert]::FromBase64String($Key)
    $date = [System.DateTime]::UtcNow.ToString("R")
    $stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`nocp-date:$date`n/$BatchAccount/jobs`napi-version:2019-08-01.10.0"
    [byte[]]$dataBytes = ([System.Text.Encoding]::UTF8).GetBytes($stringToSign)
    $hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha256.Key = [Convert]::FromBase64String($key)
    $sig = [Convert]::ToBase64String($hmacsha256.ComputeHash($dataBytes))
    $authhdr = "SharedKey $BatchAccount`:$sig"
    $headers = @{
        "ocp-date" = $date;
        "Authorization" = "$authhdr";
    }
    
    
    
    Invoke-restmethod -Headers $headers -Uri "$BatchAccountURL/jobs?api-version=2019-08-01.10.0"
    

    结果:

    【讨论】:

    • 感谢这项工作。看起来空白和后缀不正确,即使文档建议这样做。谢谢!
    猜你喜欢
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多