简体   繁体   English

如何通过 AD 身份验证使用 Azure API 格式访问 Application Insights 数据

[英]How to access Application Insights data using the Azure API format with AD authentication

I can't find an example anywhere!我在任何地方都找不到示例! The format is posted here at the Applications Insights REST API site.格式张贴在这里的应用洞察REST API的网站。 It is only the format and no example.这只是格式,没有示例。 I think I was able to follow the format, but when I tried it, I got an error message of "Authentication failed. The 'Authorization' header is missing."我想我能够遵循格式,但是当我尝试它时,我收到一条错误消息“身份验证失败。缺少‘授权’标头。” Typically, to get this token, you have to register your app in Azure AD and follow that process.通常,要获取此令牌,您必须在 Azure AD 中注册您的应用程序并遵循该过程。 I don't have an app I need registered.我没有需要注册的应用程序。 I want to use their api/app .我想使用他们的 api/app And the reason I want to use the Azure API format and not the Public API format is to get around the rate limit .我想使用 Azure API 格式而不是公共 API 格式的原因是为了绕过速率限制 We need to make requests about once a minute.我们需要大约每分钟发出一次请求。 Help!帮助!

According to your description, you need to create a Service Principle firstly, then use it to get API token message.根据您的描述,您需要先创建一个Service Principle,然后使用它来获取API token 消息。 Please refer to this link: Use portal to create an Azure Active Directory application and service principal that can access resources .请参阅此链接: 使用门户创建可以访问资源的 Azure Active Directory 应用程序和服务主体 You will get client id(app id) and client_secret.您将获得客户端 id(app id) 和 client_secret。 You could use the following script to get token(use Power Shell).您可以使用以下脚本获取令牌(使用 Power Shell)。

##get token
$TENANTID="******"
$APPID="<client_id>"
$PASSWORD="<client_secret>"
$result=Invoke-RestMethod -Uri https://login.microsoftonline.com/$TENANTID/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "$APPID"; "client_secret" = "$PASSWORD" }
$token=$result.access_token

After you get token, you need construct header message.获得令牌后,您需要构造头消息。 Like below:像下面这样:

$Headers=@{
    'authorization'="Bearer $token"
    'host'="management.azure.com"
    'contentype'='application/json'
}

Then, you could use API to get the information you want.然后,您可以使用 API 来获取您想要的信息。

Invoke-RestMethod  -Uri $url  -Headers $Headers -Method GET

Update:更新:

If you want to use Applications Insights REST API, don't need to use service principle to get token.如果要使用 Applications Insights REST API,则不需要使用服务原则来获取令牌。 You need to create a API key.您需要创建一个 API 密钥。 Please refer to this link .请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM