Microsoft.Azure.OperationalInsights returns 'Forbidden' 服务主体消息

Microsoft.Azure.OperationalInsights returns 'Forbidden' message for service principle

我正在尝试阅读 Azure 资源的见解。我得到的错误是:

Operation returned an invalid status code 'Forbidden'

服务原则已被授予'Log Analytics Reader'订阅工作区的角色。

Microsoft.Azure.Management.ResourceManager.Fluent.Authentication.AzureCredentials credentials =
 Microsoft.Azure.Management.ResourceManager.Fluent.SdkContext.AzureCredentialsFactory.FromServicePrincipal(
 "clientId", 
 "clientSecret", 
 "tenantId", 
 Microsoft.Azure.Management.ResourceManager.Fluent.AzureEnvironment.AzureGlobalCloud);

var client = new Microsoft.Azure.OperationalInsights.OperationalInsightsDataClient(credentials);
client.WorkspaceId = @"workspaceId";
var results = client.QueryWithHttpMessagesAsync("union * | take 5").Result;

的解决方案是使用委派权限,但我需要一个无需用户交互的身份验证令牌。

由于您已在订阅中授予服务原则 'Log Analytics Reader' 角色。还需要为您的服务原则授予 API 权限。

  • 去你的服务原则>API权限>添加权限 > API 我的组织使用。 搜索 Log analytics API .

  • 并将以下权限添加到您的服务原则中。

  • 向您的服务原则授予管理员同意权限以获得API权限。

一旦您授予服务原则的权限,您就可以阅读日志。

有关使用 powershell 的相同内容的更多信息,请参阅此 Blog