问题描述
我正在尝试通过访问 management.azure.com api 来获取我的订阅的定价详细信息。根据此处的文档:https://docs.microsoft.com/en-us/previous-versions/azure/reference/mt219004(v=azure.100)?redirectedfrom=MSDN
我尝试使用提供的代码、Postman 和 3rd 方库 (https://github.com/codehollow/AzureBillingApi/) 访问 API,所有这些都导致无法反序列化的错误响应。
原型代码如下:
AzureCredentials credentials = credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
clientId,clientSecret,tenantId,AzureEnvironment.AzureGlobalCloud).WithDefaultSubscription(subscriptionId);
ServiceClientCredentials serviceClientCredentials = await await ApplicationTokenProvider.LoginSilentAsync(tenantId,clientId,clientSecret);
string urlString = @$"https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Commerce/RateCard?api-version=2016-08-31-preview&$filter=OfferDurableId eq 'MS-AZR-0003p' and Currency eq 'USD' and Locale eq 'en-US' and regioninfo eq 'US' and MeterCategory eq 'Storage'";
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get,urlString))
{
await serviceClientCredentials.ProcessHttpRequestAsync(requestMessage,CancellationToken.None);
using (HttpClient client = new HttpClient())
{
HttpResponseMessage responseMessage = await client.SendAsync(requestMessage);
if(!responseMessage.IsSuccessstatusCode)
throw new InvalidOperationException(responseMessage.ReasonPhrase);
string contentString = await responseMessage.Content.ReadAsstringAsync();
RateCardResponse rateCardResponse = JsonConvert.DeserializeObject<RateCardResponse>(contentString);
}
}
虽然我得到一个有效的响应 (200),但 contentString 变量是无效的 JSON。为了便于阅读,示例已被缩短。
{
"OfferTerms": [],"Meters": [
{
"EffectiveDate": "2020-05-01T00:00:00Z","IncludedQuantity": 0.0,"MeterCategory": "Virtual Machines","MeterId": "cd2d7ca5-2d4c-5f93-94d0-8cee0662c71c","MeterName": "E20 v4","MeterRates": {
"0": 1.52
},"MeterRegion": "AP Southeast","MeterStatus": "Active","MeterSubCategory": "Ev4 Series","MeterTags": [],"Unit": "1 Hour"
},{
"EffectiveDate": "2020-11-01T00:00:00Z","MeterCategory": "Virtual Machines Licenses","MeterId": "e8ceef66-d651-5a3c-9af9-046917e3a466","MeterName": "104 vcpu License","MeterRates": {
"0": 1.456
},"MeterRegion": <ACTUAL RESPONSE IS 627,746 bytes but always cuts off at this point>
这个网站 (https://azureprice.net/) 似乎从同一个 API 中提取最近的详细信息没有问题,但我很困惑为什么我的回复被切断并且无效。
解决方法
我刚刚再次运行了该代码,无论有没有流阅读器,它都运行良好。我猜微软的人有一个问题并纠正了它。仍然没有过滤功能,但我可以做到客户端。