简体   繁体   中英

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. 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. I don't have an app I need registered. I want to use their 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 . 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. Please refer to this link: Use portal to create an Azure Active Directory application and service principal that can access resources . You will get client id(app id) and client_secret. You could use the following script to get token(use 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.

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. You need to create a API key. Please refer to this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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