尝试获取 azure 订阅费率卡和消费使用时出错
Error when trying to fetch azure subscription rate card and consumption usage
我正在尝试获取价目表和使用费,以支付我的订阅费用。但是一直有例外。尝试了多个选项,但 none 有效。选项 1 在过去曾经完美无缺地工作,但自从过去 4 周以来低于异常
==================================异常详情=========== ======================
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred.
Source=mscorlib
StackTrace:at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task1.get_Result()
Inner Exception 1:
TaskCanceledException: A task was canceled.
============== 我尝试了多种选择,但 none 有效。 ==================
选项 1 使用 Microsoft.IdentityModel.Clients.ActiveDirectory 版本 3.13.9 库和 Microsoft.Commerce api 进行身份验证 ====================
var url = "https://login.microsoftonline.com" ;
var tenant = "mytenant.onmicrosoft.com" ;
var resource = "https://management.core.windows.net" ;
var clientSecret = "xxxx";
var clientId = "client id of the app registerd in azure ad";
var authenticationContext =
new AuthenticationContext(CombineUrl(url, tenant));
ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
var authTask =
authenticationContext.AcquireTokenAsync(resource, clientCred);
authTask.Wait();
var token = authTask.Result.Token;
string offerDurableId= "MS-AZR-0003p"; // also tried with "MS-AZR-0063p"
string currency = "USD";
string locale = "en-US";
string regionInfo = "US";
string SubscriptionId = "my subscription id"
string APIVERSION = "2015-06-01-preview";
string url = string.Format(
"https://management.azure.com/subscriptions/{0}/providers" + `
"/Microsoft.Commerce/RateCard?api-version={1}" + `
"&$filter=OfferDurableId eq '{2}' and Currency eq '{3}'" + `
"and Locale eq '{4}' and RegionInfo eq '{5}'", SubscriptionId,
APIVERSION, offerDurableId, currency, locale, regionInfo); `
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
{
var httpClient = new HttpClient() `
request.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = httpClient.SendAsync(request).Result; //
throws "task Canceled" exception
var readTask = response.Content.ReadAsStringAsync(); `
readTask.Wait(); `
data= readTask.Result; `
}
============ 选项 2 使用 OAuth 库 Microsoft.Identity.Client 版本 4.31.0 和 Microsoft.Consumption API 的
var scopes = new List<string>();
var TenantId = "tenant id as shown in azure portal for my registerd app"
var loginAuthority = "https://login.microsoftonline.com";
scopes.Add("https://management.azure.com/.default");
var authResult = AuthClient.AcquireTokenForClient(scope.ToArray())
.WithAuthority(loginAuthority, TenantId)
.ExecuteAsync().GetAwaiter().GetResult();
string token = authResult.AccessToken;
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = HTTPClient.SendAsync(request).Result; // returns blank
}
```
===============================
I have been using Option 1 in the past and it always worked, not sure what broke and need help in fixing it.
不确定错误,我认为这可能是超时问题,因为返回的数据以 MB 为单位。
尝试将超时值增加到 3 分钟而不是默认值并且它有效。
' httpClient.TimeOut = new TimeSpan(0,3,0);`
经过多次测试,目前有效。
我正在尝试获取价目表和使用费,以支付我的订阅费用。但是一直有例外。尝试了多个选项,但 none 有效。选项 1 在过去曾经完美无缺地工作,但自从过去 4 周以来低于异常
==================================异常详情=========== ======================
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred.
Source=mscorlib
StackTrace:at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task1.get_Result()
Inner Exception 1:
TaskCanceledException: A task was canceled.
============== 我尝试了多种选择,但 none 有效。 ==================
选项 1 使用 Microsoft.IdentityModel.Clients.ActiveDirectory 版本 3.13.9 库和 Microsoft.Commerce api 进行身份验证 ====================
var url = "https://login.microsoftonline.com" ;
var tenant = "mytenant.onmicrosoft.com" ;
var resource = "https://management.core.windows.net" ;
var clientSecret = "xxxx";
var clientId = "client id of the app registerd in azure ad";
var authenticationContext =
new AuthenticationContext(CombineUrl(url, tenant));
ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
var authTask =
authenticationContext.AcquireTokenAsync(resource, clientCred);
authTask.Wait();
var token = authTask.Result.Token;
string offerDurableId= "MS-AZR-0003p"; // also tried with "MS-AZR-0063p"
string currency = "USD";
string locale = "en-US";
string regionInfo = "US";
string SubscriptionId = "my subscription id"
string APIVERSION = "2015-06-01-preview";
string url = string.Format(
"https://management.azure.com/subscriptions/{0}/providers" + `
"/Microsoft.Commerce/RateCard?api-version={1}" + `
"&$filter=OfferDurableId eq '{2}' and Currency eq '{3}'" + `
"and Locale eq '{4}' and RegionInfo eq '{5}'", SubscriptionId,
APIVERSION, offerDurableId, currency, locale, regionInfo); `
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
{
var httpClient = new HttpClient() `
request.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = httpClient.SendAsync(request).Result; //
throws "task Canceled" exception
var readTask = response.Content.ReadAsStringAsync(); `
readTask.Wait(); `
data= readTask.Result; `
}
============ 选项 2 使用 OAuth 库 Microsoft.Identity.Client 版本 4.31.0 和 Microsoft.Consumption API 的
var scopes = new List<string>();
var TenantId = "tenant id as shown in azure portal for my registerd app"
var loginAuthority = "https://login.microsoftonline.com";
scopes.Add("https://management.azure.com/.default");
var authResult = AuthClient.AcquireTokenForClient(scope.ToArray())
.WithAuthority(loginAuthority, TenantId)
.ExecuteAsync().GetAwaiter().GetResult();
string token = authResult.AccessToken;
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = HTTPClient.SendAsync(request).Result; // returns blank
}
```
===============================
I have been using Option 1 in the past and it always worked, not sure what broke and need help in fixing it.
不确定错误,我认为这可能是超时问题,因为返回的数据以 MB 为单位。 尝试将超时值增加到 3 分钟而不是默认值并且它有效。 ' httpClient.TimeOut = new TimeSpan(0,3,0);`
经过多次测试,目前有效。