![](/img/trans.png)
[英]PessimisticConcurrencyConflict Error While Operating On Azure Api Management Using Azure Powershell
[英]Azure API Management Report API using Powershell
我正在尝试使用 Powershell 执行 APIM Report API 但它不起作用。
这是 powershell 代码。
$gen_sas_code = @"
using System;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;
public class Sas
{
public static void GenSas()
{
var id = "integration";
var key = "myprimarykey";
var expiry = DateTime.UtcNow.AddDays(10);
using (var encoder = new HMACSHA512(Encoding.UTF8.GetBytes(key)))
{
var dataToSign = id + "\n" + expiry.ToString("O", CultureInfo.InvariantCulture);
var hash = encoder.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
var signature = Convert.ToBase64String(hash);
var encodedToken = string.Format("SharedAccessSignature uid={0}&ex={1:o}&sn={2}", id, expiry, signature);
Console.WriteLine(encodedToken);
}
}
}
"@
Add-Type -TypeDefinition $gen_sas_code -Language CSharp
$sas = [Sas]::GenSas()
$interval = "PT15M"
$start_time = "'2017-10-16T04:02:00'"
$end_time = "'2017-10-16T04:15:00'"
$report_url = "https://myapim.management.azure-api.net/reports/byTime?api-version=2017-03-01&interval=$interval&`$filter=timestamp+ge+datetime$start_time+and++timestamp+le+datetime$end_time"
$report_headers = @{ "Authorization" = "$sas" };
Invoke-RestMethod -Method Get -Uri $report_url -Headers $report_headers
我参考了这份关于报告 API 的文档( https://docs.microsoft.com/en-us/rest/api/apimanagement/apimanagementrest/azure-api-management-rest-api-report-entity#ReportByTime )。
这是错误响应。
*远程服务器返回错误:(401) Unzuthorized
Invoke-RestMethod : リモート サーバーがエラーを返しました: (401) 許可されていません*
発生場所 C:\sas.ps1:56 文字:1
+ Invoke-RestMethod -Method Get -Uri $report_url -Headers $report_headers
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod]、WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
So, I consider that the powershell code has some problems because I have already confirmed that the SAS which is generated by above code works correctly by executing Report API using curl command.
你有什么想法?
这是一个有点烦人的问题,但归结为 Powershell 中的换行处理, .net
示例中的\n
指的是换行符,因此您必须使用本机方式 Z3D265B4E1EEEF0DDF17881FA003B
var dataToSign = id + "`n" + expiry.ToString("O", CultureInfo.InvariantCulture);
... 代替:
var dataToSign = id + "\n" + expiry.ToString("O", CultureInfo.InvariantCulture);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.