Skip to content

Commit 370e1ee

Browse files
committed
Refresh REST API walkthrough
1 parent 3885473 commit 370e1ee

File tree

1 file changed

+84
-5
lines changed

1 file changed

+84
-5
lines changed

articles/azure-monitor/essentials/rest-api-walkthrough.md

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
---
22
title: Azure monitoring REST API walkthrough
33
description: How to authenticate requests and use the Azure Monitor REST API to retrieve available metric definitions and metric values.
4+
author:EdB-MSFT
45
ms.topic: conceptual
5-
ms.date: 05/09/2022
6+
ms.date: 01/30/2023
67
ms.custom: has-adal-ref, devx-track-azurepowershell
7-
ms.reviewer: robb
8+
ms.reviewer: edbaynash
89
---
910

1011
# Azure monitoring REST API walkthrough
@@ -13,14 +14,83 @@ This article shows you how to perform authentication so your code can use the [A
1314

1415
The Azure Monitor API makes it possible to programmatically retrieve the available default metric definitions, dimension values, and metric values. The data can be saved in a separate data store such as Azure SQL Database, Azure Cosmos DB, or Azure Data Lake. From there, more analysis can be performed as needed.
1516

16-
Besides working with various metric data points, the Azure Monitor API also makes it possible to list alert rules, view activity logs, and do much more. For a full list of available operations, see the [Azure Monitor REST API reference](/rest/api/monitor/).
17+
The Azure Monitor API also makes it possible to list alert rules, view activity logs. For a full list of available operations, see the [Azure Monitor REST API reference](/rest/api/monitor/).
1718

1819
## Authenticate Azure Monitor requests
1920

20-
All the tasks executed against the Azure Monitor API use the Azure Resource Manager authentication model. So, all requests must be authenticated with Azure Active Directory (Azure AD). One approach to authenticating the client application is to create an Azure AD service principal and retrieve the authentication (JWT) token.
21+
Tasks executed using the Azure Monitor API use the Azure Resource Manager authentication model. All requests must be authenticated with Azure Active Directory (Azure AD). One approach to authenticating the client application is to create an Azure AD service principal and retrieve the authentication (JWT) token.
2122

23+
24+
# [Azure Portal](#tab/portal)
25+
26+
To create an Azure AD service principal using the Azure Portal see [Register an App to request authorization tokens and work with APIs](../logs/api/register-app-for-token)
27+
28+
29+
# [Azure CLI](#tab/cli)
30+
31+
Run the following script to create a service principal and app.
32+
33+
```azurecli
34+
ad sp create-for-rbac -n <Service principal display name>
35+
36+
```
37+
The response looks as follows:
38+
```JSON
39+
{
40+
"appId": "0a123b56-c987-1234-abcd-1a2b3c4d5e6f",
41+
"displayName": "AzMonAPIApp",
42+
"password": "123456.ABCDE.~XYZ876123ABcEdB7169",
43+
"tenant": "a1234bcd-5849-4a5d-a2eb-5267eae1bbc7"
44+
}
45+
46+
```
47+
>[!Important]
48+
> The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control.
49+
50+
For more information on creating a service principal using Azure CLI, see [AA](https://learn.microsoft.com/cli/azure/create-an-azure-service-principal-azure-cli)
51+
52+
To retrieve an access token using a REST call submit the following request using the `appId` and `password`:
53+
54+
```http
55+
56+
POST /<appId>/oauth2/v2.0/token
57+
Host: https://login.microsoftonline.com
58+
Content-Type: application/x-www-form-urlencoded
59+
60+
grant_type=client_credentials
61+
&client_id=<app-client-id>
62+
&resource=https://management.azure.com
63+
&client_secret=<password>
64+
65+
```
66+
67+
For example
68+
69+
```bash
70+
curl --location --request POST 'https://login.microsoftonline.com/a1234bcd-5849-4a5d-a2eb-5267eae1bbc7/oauth2/token' \
71+
--header 'Content-Type: application/x-www-form-urlencoded' \
72+
--data-urlencode 'grant_type=client_credentials' \
73+
--data-urlencode 'client_id=0a123b56-c987-1234-abcd-1a2b3c4d5e6f' \
74+
--data-urlencode 'client_secret123456.ABCDE.~XYZ876123ABceDb0000' \
75+
--data-urlencode 'resource=https://management.azure.com'
76+
77+
```
78+
A successful request receives an access token in the response:
79+
80+
```http
81+
{
82+
token_type": "Bearer",
83+
"expires_in": "86399",
84+
"ext_expires_in": "86399",
85+
"access_token": ""eyJ0eXAiOiJKV1QiLCJ.....Ax"
86+
}
87+
```
88+
Use the access token in your Azure Monitor API requests
89+
90+
### [Powershell](#tab/powershell)
2291
The following sample script demonstrates creating an Azure AD service principal via PowerShell. For a more detailed walkthrough, see the documentation on [using Azure PowerShell to create a service principal to access resources](/powershell/azure/create-azure-service-principal-azureps). It's also possible to [create a service principal via the Azure portal](../../active-directory/develop/howto-create-service-principal-portal.md).
2392

93+
2494
```powershell
2595
$subscriptionId = "{azure-subscription-id}"
2696
$resourceGroupName = "{resource-group-name}"
@@ -130,7 +200,16 @@ Write-Host "Access Token: " $myvar.AccessToken
130200

131201
Loading the certificate from a .pfx file in PowerShell can make it easier for an admin to manage certificates without having to install the certificate in the certificate store. However, this step shouldn't be done on a client machine because the user could potentially discover the file and the password for it and the method to authenticate. The client credentials flow is only intended to be run in a back-end service-to-service type of scenario where only admins have access to the machine.
132202

133-
After authenticating, queries can then be executed against the Azure Monitor REST API. There are two helpful queries:
203+
---
204+
205+
206+
## Roles
207+
Assign role if necessary
208+
209+
210+
211+
212+
After authenticating and retrieving a token, queries can then be executed against the Azure Monitor REST API. There are two helpful queries:
134213

135214
- List the metric definitions for a resource.
136215
- Retrieve the metric values.

0 commit comments

Comments
 (0)