提问人:BrettL 提问时间:10/22/2023 最后编辑:BrettL 更新时间:10/25/2023 访问量:62
使用 C# 和新的 Azure.ResourceManager.Billing 命名空间获取 Azure 中的计费帐户列表
Getting list of billing accounts in Azure using C# and new Azure.ResourceManager.Billing Namespace
问:
我正在尝试在 C# 中使用新的 Azure 资源 SDK,但我只是没有做对。旧的已弃用命名空间 Microsoft.Azure.Management.Billing 命名空间很容易实现这一点,即:
var clientCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var billingClient = new BillingManagementClient(clientCredential);
var billingAccounts = await billingClient.BillingAccounts.ListAsync();
但是,我需要将其迁移到较新的 Azure.ResourceManager.Billing 命名空间。
使用新的 SDK 连接我没有问题,即:
var scopes = new[] { "https://management.core.windows.net/.default" };
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientSecretCredential = new ClientSecretCredential(tenantIdOrName, clientId, clientSecret, options);
client = new ArmClient(clientSecretCredential);
然后获取订阅对象,即SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
但这就是我迷路的地方。我在 SDK 文档中找到的所有计费方法似乎都需要知道要导出/报告的计费帐户。我只是无法弄清楚拥有客户端/订阅对象实例与获取计费帐户列表或如何链接执行请求所需的“资源”之间的差距。 我导入了以下命名空间:
using Azure.Core;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Billing;
using Azure.ResourceManager.Billing.Models;
并查看了以下 MS 引用:resourcemanager.billing-readme、azuresdk-net-mgmt-sample、GetUsageDetails 和许多其他参考资料。(最后一个尝试解决它,查看另一个命名空间区域。这是我应该去的方向吗,即调用 HTTPS 端点,因为目前没有方法可以在 SDK 中执行我需要的操作? 任何帮助将不胜感激,即使是对任何 SDK 文档的良好参考,该文档具有比当前 Microsoft 文档更好的指南。
答:
0赞
SaiVamshiKrishna
10/25/2023
#1
我尝试使用以下代码来获取计费的使用详细信息,它成功工作如下:-
法典:-
//Sample Code
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager.Consumption;
using Azure.ResourceManager.Consumption.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace AzureCostReport
{
class Program
{
// Update the below constants with your Azure subscription ID, tenant ID, client ID, and client secret.
private const string subscriptionId = "xxxxx2a7";
private const string tenantId = "xxxxxxxxf9038592395";
private const string clientId = "xxxxx31435cb";
private const string clientSecret = "xxxxxxxA1zlgc~A";
static async Task Main(string[] args)
{
// Authenticate using a service principal.
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Get the bearer token
var token = await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://management.azure.com/.default" }));
// Get the Cost details
GetCostDetails(token).Wait();
Console.ReadLine();
}
private static async Task GetCostDetails(AccessToken token)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);
//Invoke te Consumption details REST API
var result = await client.GetAsync("https://management.azure.com/subscriptions/xxxxxxx7/providers/Microsoft.Consumption/usageDetails?startDate=2023-10-01&endDate=2023-10-23&$top=1000&api-version=2017-06-30-preview");
Console.WriteLine(await result.Content.ReadAsStringAsync());
}
}
}
输出:-
Code2 检索计费帐户:-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Replace these with your Azure AD application registration details and subscription ID.
string subscriptionId = "xxxxx2a7";
string tenantId = "xxxxx395";
string clientId = "xxxxx35cb";
string clientSecret = "xxxxxxA1zlgc~A";
string authority = $"https://login.microsoftonline.com/{tenantId}";
string resource = "https://management.azure.com/";
string apiUrl = $"https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Billing/billingAccounts?api-version=2019-10-01-preview";
var authenticationContext = new AuthenticationContext(authority);
var credential = new ClientCredential(clientId, clientSecret);
var authenticationResult = await authenticationContext.AcquireTokenAsync(resource, credential);
if (authenticationResult == null)
{
Console.WriteLine("Authentication failed.");
return;
}
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {authenticationResult.AccessToken}");
var response = await httpClient.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
else
{
Console.WriteLine($"Failed to retrieve billing accounts. Status code: {response.StatusCode}");
}
}
}
评论
0赞
BrettL
10/25/2023
非常感谢。唯一的问题是我一直收到 404 错误。我相信计费的 URL 实际上是 所以没有订阅者详细信息。这将返回 200 个响应,但没有值,因此必须了解为什么会这样,除非 URL 仍然不正确。https://management.azure.com/providers/Microsoft.Billing/billingAccounts?api-version=2019-10-01-preview
0赞
BrettL
10/25/2023
好的,它现在可以工作了。需要向 Azure 中“成本管理”部分下的服务主体授予“读取者”权限。
评论