【发布时间】:2021-01-06 21:24:11
【问题描述】:
我正在尝试通过访问 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",
"IncludedQuantity": 0.0,
"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 中提取最近的详细信息,但我很困惑为什么我的回复被截断且无效。
【问题讨论】:
-
即使尝试读取为流,响应也是无效的。结果相同:设置 MeterRegion 的值时意外结束。路径 'Meters[1941].MeterRegion',第 1 行,位置 627736。
标签: c# azure ratecard-api