You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -13,14 +14,83 @@ This article shows you how to perform authentication so your code can use the [A
13
14
14
15
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.
15
16
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/).
17
18
18
19
## Authenticate Azure Monitor requests
19
20
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.
21
22
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' \
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)
22
91
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).
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.
132
202
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:
0 commit comments