Microsoft.Azure.Management.Consumption .NET 包和 ApiVersion

Microsoft.Azure.Management.Consumption .NET package and ApiVersion

我正在尝试使用 Microsoft.Azure.Management.Consumption 3.0.2 包来访问使用和消耗数据。

但是在调用 UsageDetails.List 时出现以下错误:

Subscription scope usage is not supported for current api version. Please use api version after 2019-10-01

是否有支持此版本的软件包的新版本(或预计会有)?

在此期间我有什么选择?

我可以直接使用 GET 来对抗

https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.Consumption/usageDetails?api-version=2019-10-01

但是还有其他选择吗?

更新了代码

请求代码:

AuthenticationResult result = GetToken();
Microsoft.Rest.TokenCredentials tokenCredentials = new 
Microsoft.Rest.TokenCredentials(result.AccessToken);
ConsumptionManagementClient client = new 
ConsumptionManagementClient(tokenCredentials);
client.SubscriptionId = "SubscriptionId I would like to check"; 

var usage = client.UsageDetails.List(); // Exception here with API Version

授权码示例:

private static AuthenticationResult GetToken()
{
    string clientId = "MyappId";

    string[] scopes = new string[] { "https://management.azure.com/.default" };
    var app = PublicClientApplicationBuilder
        .Create(clientId)
        .WithRedirectUri("https://localhost")
        .WithTenantId("TenantId I want to check")
        .Build();
    var task = app.GetAccountsAsync();
    task.Wait();
    var accounts = task.Result;
    try
    {
        var task1 = app.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
        task1.Wait();
        return task1.Result;
    }
    catch (MsalUiRequiredException)
    {
        var task2 = app.AcquireTokenInteractive(scopes).ExecuteAsync();
        task2.Wait();
        return task2.Result;
    }
}

根据我的研究,我们可以初始化 ServiceClientCredentials 来创建 ConsumptionManagementClient 并且 ServiceClientCredentials 有方法 ProcessHttpRequestAsync 我们可以使用它来重新创建请求

例如

  1. 实施ServiceClientCredentials
class CustomLoginCredentials : ServiceClientCredentials
    {
       
        private string AuthenticationToken { get; set; }
        public override void InitializeServiceClient<T>(ServiceClient<T> client)
        {
           string clientId = "MyappId";

    string[] scopes = new string[] { "https://management.azure.com/.default" };
    var app = PublicClientApplicationBuilder
        .Create(clientId)
        .WithRedirectUri("https://localhost")
        .WithTenantId("TenantId I want to check")
        .Build();
            var task = app.GetAccountsAsync();
            task.Wait();
            var accounts = task.Result;
            try
            {
                var task1 = app.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
                task1.Wait();
                AuthenticationToken= task1.Result.AccessToken;
            }
            catch (MsalUiRequiredException)
            {
                var task2 = app.AcquireTokenInteractive(scopes).ExecuteAsync();
                task2.Wait();
                AuthenticationToken= task2.Result.AccessToken;
            }
            
        }
        public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (AuthenticationToken == null)
            {
                throw new InvalidOperationException("Token Provider Cannot Be Null");
            }

            var url = request.RequestUri.ToString().Split('?')[0] + "?";
            var querys = request.RequestUri.ToString().Split('?')[1].Split('&');
            for (var i = 1; i <= querys.Length; i++)
            {

                if (querys[i - 1].StartsWith("api-version"))
                {
                    url += "api-version=2019-10-01";
                }
                else
                {
                    url += querys[i - 1];
                }


                if (i < querys.Length)
                {

                    url += "&";
                }



            }

            Console.WriteLine(url);

            request.RequestUri = new Uri(url);

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);



            await base.ProcessHttpRequestAsync(request, cancellationToken);

        }



    }
  1. 获取详细信息
 var cred = new CustomLoginCredentials();
ConsumptionManagementClient client = new ConsumptionManagementClient(cred);
client.SubscriptionId = "SubscriptionId I would like to check"; 
var usage = client.UsageDetails.List();