操作返回无效状态代码 'BadRequest' - Microsoft.Azure.Management.ResourceGraph
Operation returned an invalid status code 'BadRequest' - Microsoft.Azure.Management.ResourceGraph
每当 var response
行执行时都会抛出一个异常
Operation returned an invalid status code 'BadRequest'
我在 Azure Resource Graph Explorer 中执行的相同请求按预期工作。但出于某种奇怪的原因,当我在 .net 控制台应用程序 运行 中执行此操作时,它告诉我这是一个错误的请求,而实际上它是正确的请求。但是,在显示该错误后,它显示 Principal used: IsAuthenticated:True Type:User TenantId: xxxxx UserPrincipalName: xxxx
已安装的软件包:
<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
Program.cs
using Microsoft.Azure.Management.ResourceGraph;
using Microsoft.Azure.Management.ResourceGraph.Models;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Rest;
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
RunResourceGraphQuery(azureServiceTokenProvider).Wait();
if (azureServiceTokenProvider.PrincipalUsed != null)
{
Console.WriteLine($"{Environment.NewLine}Principal used: {azureServiceTokenProvider.PrincipalUsed}");
}
Console.ReadLine();
static async Task RunResourceGraphQuery(AzureServiceTokenProvider azureServiceTokenProvider)
{
Console.WriteLine($"{Environment.NewLine}{Environment.NewLine}Please enter the subscription Id");
var subscriptionId = Console.ReadLine();
List<string> subscriptions = new();
subscriptions.Add(subscriptionId);
try
{
var tokenCredentials = new TokenCredentials(await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false));
var resourceGraphClient = new ResourceGraphClient(tokenCredentials);
var userQueryRequest = new QueryRequest(subscriptions: subscriptions, query: "resources | project name, ['type'] | limit 5", null, null);
var response = resourceGraphClient.Resources(userQueryRequest);
Console.WriteLine(response);
}
catch (Exception exp)
{
Console.WriteLine($"Error message: {exp.Message}");
}
}
我已尝试在我的环境中重现上述问题,但我遇到了与您遇到的问题相同的问题。
使用带有 .net6
的控制台应用程序创建了一个新项目,并将您的代码复制到我的本地。
已安装 nuget packages
:-
Microsoft.Azure.Management.ResourceGraph" 版本="2.1.0"
Microsoft.Azure.Services.AppAuthentication" 版本="1.6.2"
这是我的.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
</ItemGroup>
</Project>
当我尝试使用您的代码时,我得到 Operation returned an invalid status code 'BadRequest'
:
修改查询参数类型后query: "resources | project name, ['type'] | limit 5"
改为query: "Resources | project name, type | limit 5"
Program.cs:-
using Microsoft.Azure.Management.ResourceGraph;
using Microsoft.Azure.Management.ResourceGraph.Models;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Rest;
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
RunResourceGraphQuery(azureServiceTokenProvider).Wait();
if (azureServiceTokenProvider.PrincipalUsed != null)
{
Console.WriteLine($"{Environment.NewLine}Principal used: {azureServiceTokenProvider.PrincipalUsed}");
}
Console.ReadLine();
static async Task RunResourceGraphQuery(AzureServiceTokenProvider azureServiceTokenProvider)
{
Console.WriteLine($"{Environment.NewLine}{Environment.NewLine}Please enter the subscription Id");
var subscriptionId = Console.ReadLine();
List<string> subscriptions = new();
subscriptions.Add(subscriptionId);
try
{
var tokenCredentials = new TokenCredentials(await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false));
var resourceGraphClient = new ResourceGraphClient(tokenCredentials);
var userQueryRequest = new QueryRequest(subscriptions: subscriptions, query: "Resources | project name, type | limit 5", null, null);
var response = resourceGraphClient.Resources(userQueryRequest);
Console.WriteLine(response);
}
catch (Exception exp)
{
Console.WriteLine($"Error message: {exp.Message}");
}
}
它在我这边运行良好:
如果您遇到同样的问题,请参阅此 Microsoft 文档:Run your first Resource Graph query using .NET Core
有关更多信息,请参阅此 MICROSOFT PLAYGROUND:Connecting to the Resource Graph Client via the Resource Graph SDK
每当 var response
行执行时都会抛出一个异常
Operation returned an invalid status code 'BadRequest'
我在 Azure Resource Graph Explorer 中执行的相同请求按预期工作。但出于某种奇怪的原因,当我在 .net 控制台应用程序 运行 中执行此操作时,它告诉我这是一个错误的请求,而实际上它是正确的请求。但是,在显示该错误后,它显示 Principal used: IsAuthenticated:True Type:User TenantId: xxxxx UserPrincipalName: xxxx
已安装的软件包:
<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
Program.cs
using Microsoft.Azure.Management.ResourceGraph;
using Microsoft.Azure.Management.ResourceGraph.Models;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Rest;
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
RunResourceGraphQuery(azureServiceTokenProvider).Wait();
if (azureServiceTokenProvider.PrincipalUsed != null)
{
Console.WriteLine($"{Environment.NewLine}Principal used: {azureServiceTokenProvider.PrincipalUsed}");
}
Console.ReadLine();
static async Task RunResourceGraphQuery(AzureServiceTokenProvider azureServiceTokenProvider)
{
Console.WriteLine($"{Environment.NewLine}{Environment.NewLine}Please enter the subscription Id");
var subscriptionId = Console.ReadLine();
List<string> subscriptions = new();
subscriptions.Add(subscriptionId);
try
{
var tokenCredentials = new TokenCredentials(await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false));
var resourceGraphClient = new ResourceGraphClient(tokenCredentials);
var userQueryRequest = new QueryRequest(subscriptions: subscriptions, query: "resources | project name, ['type'] | limit 5", null, null);
var response = resourceGraphClient.Resources(userQueryRequest);
Console.WriteLine(response);
}
catch (Exception exp)
{
Console.WriteLine($"Error message: {exp.Message}");
}
}
我已尝试在我的环境中重现上述问题,但我遇到了与您遇到的问题相同的问题。
使用带有 .net6
的控制台应用程序创建了一个新项目,并将您的代码复制到我的本地。
已安装 nuget packages
:-
Microsoft.Azure.Management.ResourceGraph" 版本="2.1.0" Microsoft.Azure.Services.AppAuthentication" 版本="1.6.2"
这是我的.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
</ItemGroup>
</Project>
当我尝试使用您的代码时,我得到 Operation returned an invalid status code 'BadRequest'
:
修改查询参数类型后query: "resources | project name, ['type'] | limit 5"
改为query: "Resources | project name, type | limit 5"
Program.cs:-
using Microsoft.Azure.Management.ResourceGraph;
using Microsoft.Azure.Management.ResourceGraph.Models;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Rest;
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
RunResourceGraphQuery(azureServiceTokenProvider).Wait();
if (azureServiceTokenProvider.PrincipalUsed != null)
{
Console.WriteLine($"{Environment.NewLine}Principal used: {azureServiceTokenProvider.PrincipalUsed}");
}
Console.ReadLine();
static async Task RunResourceGraphQuery(AzureServiceTokenProvider azureServiceTokenProvider)
{
Console.WriteLine($"{Environment.NewLine}{Environment.NewLine}Please enter the subscription Id");
var subscriptionId = Console.ReadLine();
List<string> subscriptions = new();
subscriptions.Add(subscriptionId);
try
{
var tokenCredentials = new TokenCredentials(await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false));
var resourceGraphClient = new ResourceGraphClient(tokenCredentials);
var userQueryRequest = new QueryRequest(subscriptions: subscriptions, query: "Resources | project name, type | limit 5", null, null);
var response = resourceGraphClient.Resources(userQueryRequest);
Console.WriteLine(response);
}
catch (Exception exp)
{
Console.WriteLine($"Error message: {exp.Message}");
}
}
它在我这边运行良好:
如果您遇到同样的问题,请参阅此 Microsoft 文档:Run your first Resource Graph query using .NET Core
有关更多信息,请参阅此 MICROSOFT PLAYGROUND:Connecting to the Resource Graph Client via the Resource Graph SDK