当我在 azure docs post man 上尝试 azure rest api url 时,我能够获得一个包含所有资源组的 json。 https://docs.microsoft.com/en-us/rest/api/resources/resourcegroups/list#code-try-0链接
但我正在从 ASP.NET MVC Core C# 应用程序中尝试它,我收到一个空数组。
public async Task<ResourceGroupModel> GetResourceGroupStatus()
{
ResourceGroupModel resourceGroupModel = null;
try
{
string requestUrlString = iconfiguration.GetValue<string>("HealthSettings:AzureGetResourcesBySubscriptionURL");
string azureSubscription = iconfiguration.GetValue<string>("HealthSettings:AzureSubscription");
string clientId = iconfiguration.GetValue<string>("HealthSettings:ClientId");
string tenantId = iconfiguration.GetValue<string>("HealthSettings:TenantId");
string clientSecret = iconfiguration.GetValue<string>("HealthSettings:ClientSecret");
Uri requestUrl = new Uri(requestUrlString.Replace("{subscriptionId}", azureSubscription));
string token = await GetAccessToken(tenantId, clientId, clientSecret);
_httpClient.DefaultRequestHeaders.Remove("Authorization");
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var response = _httpClient.GetAsync(requestUrl);
if (response.Result.IsSuccessStatusCode)
{
var data = response.Result.Content.ReadAsStringAsync();
resourceGroupModel = ResourceGroupModel.FromJson(data.Result.ToString());
}
}
catch (Exception ex)
{
}
return resourceGroupModel;
}
你能帮我摆脱 Azure REST API 的奇怪行为吗?提前谢谢了。:)