首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Microsoft.Azure.Management.Consumption .NET软件包和ApiVersion

Microsoft.Azure.Management.Consumption .NET软件包和ApiVersion
EN

Stack Overflow用户
提问于 2020-08-26 07:58:16
回答 1查看 412关注 0票数 1

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

然而,在对UsageDetails.List的调用中,我得到了以下错误:

当前api版本不支持订阅范围使用。请在2019-10-01之后使用api版本。

是否有支持此版本的包的新版本(或预期的版本)?

在此期间我还有别的选择吗?

我可以直接用

代码语言:javascript
复制
https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.Consumption/usageDetails?api-version=2019-10-01

但还有其他选择吗?

用代码更新

请求代码:

代码语言:javascript
复制
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

Auth代码示例:

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-28 02:35:54

根据我的研究,我们可以初始化ServiceClientCredentials来创建ConsumptionManagementClientServiceClientCredentials有方法ProcessHttpRequestAsync,可以用来重新创建请求。

例如

  1. 实现ServiceClientCredentials
代码语言:javascript
复制
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. 获取细节
代码语言:javascript
复制
 var cred = new CustomLoginCredentials();
ConsumptionManagementClient client = new ConsumptionManagementClient(cred);
client.SubscriptionId = "SubscriptionId I would like to check"; 
var usage = client.UsageDetails.List();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63593089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档