尝试获取azure订阅费率卡和消费使用时出错
我正在为我的现收现付订阅取ratecard和uage。然而,不断地得到例外。尝试了多个选项,但均无效。选项1在过去一直工作得很完美,但从过去4周开始,情况就变得异常了 ===================================================异常详细信息=================================尝试获取azure订阅费率卡和消费使用时出错,azure,azure-management-api,microsoft-commerce-server,ratecard-api,Azure,Azure Management Api,Microsoft Commerce Server,Ratecard Api,我正在为我的现收现付订阅取ratecard和uage。然而,不断地得到例外。尝试了多个选项,但均无效。选项1在过去一直工作得很完美,但从过去4周开始,情况就变得异常了 ===================================================异常详细信息================================= System.AggregateException HResult=0x80131500 Message=One or more errors occ
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.
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; `
}
================我尝试了多个选项,但都不起作用==================
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.
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; `
}
选项1使用Microsoft.IdentityModel.Clients.ActiveDirectory版本3.13.9库和Microsoft.Commerce api进行身份验证====================
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.
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; `
}
==========使用OAuth库Microsoft.Identity.Client版本4.31.0和Microsoft.Consumption API的选项2
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.
var scopes=新列表();
var TenantId=“我的registerd应用程序的azure门户中显示的租户id”
var登录权限=”https://login.microsoftonline.com";
作用域。添加(“https://management.azure.com/.default");
var authResult=AuthClient.AcquireTokenForClient(scope.ToArray())
.具有权限(登录权限、租户)
.ExecuteAsync().GetAwaiter().GetResult();
字符串令牌=authResult.AccessToken;
使用(var request=newhttprequestmessage(HttpMethod.Get,url))
{
request.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,令牌);
HttpResponseMessage response=HTTPClient.SendAsync(请求).Result;//返回空白
}
```
===============================
我过去一直在使用选项1,但它总是有效的,不确定是什么坏了,需要帮助来修复它。
我不确定该错误,我假设可能是超时问题,因为返回的数据以MB为单位。
尝试将超时值增加到3分钟而不是默认值,结果成功了。
'httpClient.TimeOut=新的时间跨度(0,3,0)`
经过多次测试,目前仍有效