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
Copy file name to clipboardExpand all lines: articles/azure-monitor/logs/api/register-app-for-token.md
+72-4Lines changed: 72 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,9 @@ To access Azure REST APIs such as the Log analytics API, or to send custom metri
13
13
14
14
## Register an App
15
15
16
+
Create a service principal and register an app using the Azure portal, Azure CLI, or PowerShell.
17
+
### [Azure portal](#tab/portal)
18
+
16
19
1. To register an app, open the Active Directory Overview page in the Azure portal.
17
20
18
21
1. Select **App registrations** from the side bar.
@@ -38,18 +41,83 @@ To access Azure REST APIs such as the Log analytics API, or to send custom metri
38
41
:::image type="content" source="../media/api-register-app/client-secret.png" alt-text="A screenshot showing the client secrets page.":::
39
42
40
43
44
+
### [Azure CLI](#tab/cli)
45
+
46
+
47
+
Run the following script to create a service principal and app.
48
+
49
+
```azurecli
50
+
az ad sp create-for-rbac -n <Service principal display name>
51
+
52
+
```
53
+
The response looks as follows:
54
+
```JSON
55
+
{
56
+
"appId": "0a123b56-c987-1234-abcd-1a2b3c4d5e6f",
57
+
"displayName": "AzMonAPIApp",
58
+
"password": "123456.ABCDE.~XYZ876123ABcEdB7169",
59
+
"tenant": "a1234bcd-5849-4a5d-a2eb-5267eae1bbc7"
60
+
}
61
+
62
+
```
63
+
>[!Important]
64
+
> 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.
65
+
66
+
Add a role and scope for the resources that you want to access using the API
67
+
68
+
```azurecli
69
+
az role assignment create --assignee <`appId`> --role <Role> --scope <resource URI>
70
+
```
71
+
72
+
The CLI following example assigns the `Reader` role to the service principal for all resources in the `rg-001`resource group:
73
+
74
+
```azurecli
75
+
az role assignment create --assignee 0a123b56-c987-1234-abcd-1a2b3c4d5e6f --role Reader --scope '\/subscriptions/a1234bcd-5849-4a5d-a2eb-5267eae1bbc7/resourceGroups/rg-001'
76
+
```
77
+
For more information on creating a service principal using Azure CLI, see [Create an Azure service principal with the Azure CLI](https://learn.microsoft.com/cli/azure/create-an-azure-service-principal-azure-cli)
78
+
79
+
### [PowerShell](#tab/powershell)
80
+
The following sample script demonstrates creating an Azure Active Directory service principal via PowerShell. For a more detailed walkthrough, see [using Azure PowerShell to create a service principal to access resources](../../../active-directory/develop/howto-authenticate-service-principal-powershell.md)
Before you can generate a token using your app, client ID, and secret, assign the app to a role using Access control (IAM) for resource that you want to access.
44
-
The role will depend on the resource type and the API that you want to use.
112
+
Before you can generate a token using your app, client ID, and secret, assign the app to a role using Access control (IAM) for resource that you want to access. The role will depend on the resource type and the API that you want to use.
45
113
For example,
46
114
- To grant your app read from a Log Analytics Workspace, add your app as a member to the **Reader** role using Access control (IAM) for your Log Analytics Workspace. For more information, see [Access the API](./access-api.md)
47
115
48
116
- To grant access to send custom metrics for a resource, add your app as a member to the **Monitoring Metrics Publisher** role using Access control (IAM) for your resource. For more information, see [ Send metrics to the Azure Monitor metric database using REST API](../../essentials/metrics-store-custom-rest-api.md)
49
117
50
-
For more information see [Assign Azure roles using the Azure portal](https://learn.microsoft.com/azure/role-based-access-control/role-assignments-portal)
118
+
For more information, see [Assign Azure roles using the Azure portal](../../../role-based-access-control/role-assignments-portal.md)
51
119
52
-
Once you have assigned a role you can use your app, client ID, and client secret to generate a bearer token to access the REST API.
120
+
Once you've assigned a role, you can use your app, client ID, and client secret to generate a bearer token to access the REST API.
53
121
54
122
> [!NOTE]
55
123
> When using Azure AD authentication, it may take up to 60 minutes for the Azure Application Insights REST API to recognize new role-based access control (RBAC) permissions. While permissions are propagating, REST API calls may fail with error code 403.
0 commit comments