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/access-api.md
+81-76Lines changed: 81 additions & 76 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,31 +1,33 @@
1
1
---
2
-
title: API Access and Authentication
3
-
description: How to Authenticate and access the Azure Monitor Log Analytics API.
2
+
title: API access and authentication
3
+
description: Learn how to authenticate and access the Azure Monitor Log Analytics API.
4
4
author: AbbyMSFT
5
5
ms.author: abbyweisberg
6
6
ms.date: 11/28/2022
7
7
ms.topic: article
8
8
---
9
9
# Access the Azure Monitor Log Analytics API
10
10
11
-
You can submit a query request to a workspace using the Azure Monitor Log Analytics endpoint `https://api.loganalytics.azure.com`. To access the endpoint, you must authenticate through Azure Active Directory (Azure AD).
11
+
You can submit a query request to a workspace by using the Azure Monitor Log Analytics endpoint `https://api.loganalytics.azure.com`. To access the endpoint, you must authenticate through Azure Active Directory (Azure AD).
12
+
12
13
>[!Note]
13
-
> The `api.loganalytics.io` endpoint is being replaced by `api.loganalytics.azure.com`. `api.loganalytics.io` will continue to be supported for the forseeable future.
14
-
## Authenticating with a demo API key
14
+
> The `api.loganalytics.io` endpoint is being replaced by `api.loganalytics.azure.com`. The `api.loganalytics.io` endpoint will continue to be supported for the forseeable future.
15
+
16
+
## Authenticate with a demo API key
15
17
16
-
To quickly explore the API without Azure Active Directory authentication, use the demonstration workspace with sample data, which supports API key authentication.
18
+
To quickly explore the API without Azure AD authentication, use the demonstration workspace with sample data, which supports API key authentication.
17
19
18
20
To authenticate and run queries against the sample workspace, use `DEMO_WORKSPACE` as the {workspace-id} and pass in the API key `DEMO_KEY`.
19
21
20
-
If either the Application ID or the API key is incorrect, the API service will return a [403](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error) (Forbidden) error.
22
+
If either the Application ID or the API key is incorrect, the API service returns a [403](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error) (Forbidden) error.
21
23
22
-
The API key `DEMO_KEY` can be passed in three different ways, depending on whether you prefer to use the URL, a header, or basic authentication.
24
+
The API key `DEMO_KEY` can be passed in three different ways, depending on whether you want to use a header, the URL, or basic authentication:
23
25
24
-
1.**Custom header**: provide the API key in the custom header `X-Api-Key`
25
-
2.**Query parameter**: provide the API key in the URL parameter `api_key`
26
-
3.**Basic authentication**: provide the API key as either username or password. If you provide both, the API key must be in the username.
26
+
-**Custom header**: Provide the API key in the custom header `X-Api-Key`.
27
+
-**Query parameter**: Provide the API key in the URL parameter `api_key`.
28
+
-**Basic authentication**: Provide the API key as either username or password. If you provide both, the API key must be in the username.
27
29
28
-
This example uses the Workspace ID and API key in the header:
30
+
This example uses the workspace ID and API key in the header:
29
31
30
32
```
31
33
POST https://api.loganalytics.azure.com/v1/workspaces/DEMO_WORKSPACE/query
@@ -36,6 +38,7 @@ This example uses the Workspace ID and API key in the header:
36
38
"query": "AzureActivity | summarize count() by Category"
To access the API, you need to register a client app with Azure Active Directory and request a token.
64
-
1.[Register an app in Azure Active Directory](./register-app-for-token.md).
65
+
## Set up authentication
66
+
67
+
To access the API, you register a client app with Azure AD and request a token.
68
+
69
+
1.[Register an app in Azure AD](./register-app-for-token.md).
70
+
71
+
1. On the app's overview page, select **API permissions**.
72
+
1. Select **Add a permission**.
73
+
1. On the **APIs my organization uses** tab, search for **Log Analytics** and select **Log Analytics API** from the list.
65
74
66
-
1. On the app's overview page, select **API permissions**
67
-
1. Select **Add a permission**
68
-
1. In the **APIs my organization uses** tab search for *log analytics* and select **Log Analytics API** from the list.
69
-
:::image type="content" source="../media/api-register-app/request-api-permissions.png" alt-text="A screenshot showing the Request API permissions page.":::
75
+
:::image type="content" source="../media/api-register-app/request-api-permissions.png" alt-text="A screenshot that shows the Request API permissions page.":::
70
76
71
-
1. Select **Delegated permissions**
72
-
1. Check the checkbox for **Data.Read**
73
-
1. Select **Add permissions**
74
-
:::image type="content" source="../media/api-register-app/add-requested-permissions.png" alt-text="A screenshot showing the continuation of the Request API permissions page.":::
77
+
1. Select **Delegated permissions**.
78
+
1. Select the **Data.Read** checkbox.
79
+
1. Select **Add permissions**.
75
80
76
-
Now that your app is registered and has permissions to use the API, grant your app access to your Log Analytics Workspace.
81
+
:::image type="content" source="../media/api-register-app/add-requested-permissions.png" alt-text="A screenshot that shows the continuation of the Request API permissions page.":::
77
82
78
-
1. From your Log analytics Workspace overview page, select **Access control (IAM)**.
83
+
Now that your app is registered and has permissions to use the API, grant your app access to your Log Analytics workspace.
84
+
85
+
1. From your **Log Analytics workspace** overview page, select **Access control (IAM)**.
79
86
1. Select **Add role assignment**.
80
87
81
-
:::image type="content" source="../media/api-register-app/workspace-access-control.png" alt-text="A screenshot showing the access control page for a log analytics workspace.":::
88
+
:::image type="content" source="../media/api-register-app/workspace-access-control.png" alt-text="A screenshot that shows the Access control page for a Log Analytics workspace.":::
82
89
83
-
1. Select the **Reader** role then select **Members**
84
-
85
-
:::image type="content" source="../media/api-register-app/add-role-assignment.png" alt-text="A screenshot showing the add role assignment page for a log analytics workspace.":::
90
+
1. Select the **Reader** role and then select **Members**.
91
+
92
+
:::image type="content" source="../media/api-register-app/add-role-assignment.png" alt-text="A screenshot that shows the Add role assignment page for a Log Analytics workspace.":::
86
93
87
-
1. In the Members tab, select **Select members**
88
-
1. Enter the name of your app in the **Select** field.
89
-
1. Choose your app and select **Select**
90
-
1. Select **Review and assign**
91
-
92
-
:::image type="content" source="../media/api-register-app/select-members.png" alt-text="A screenshot showing the select members blade on the role assignment page for a log analytics workspace.":::
94
+
1. On the **Members** tab, choose **Select members**.
95
+
1. Enter the name of your app in the **Select** box.
96
+
1. Select your app and choose **Select**.
97
+
1. Select **Review + assign**.
93
98
94
-
1. After completing the Active Directory setup and workspace permissions, request an authorization token.
99
+
:::image type="content" source="../media/api-register-app/select-members.png" alt-text="A screenshot that shows the Select members pane on the Add role assignment page for a Log Analytics workspace.":::
100
+
101
+
1. After you finish the Active Directory setup and workspace permissions, request an authorization token.
95
102
96
103
>[!Note]
97
-
> For this example we applied the **Reader** role. This role is one of many built-in roles and may include more permissions than you require. More granular roles and permissions can be created. For more information, see [Manage access to Log Analytics workspaces](../../logs/manage-access.md).
104
+
> For this example, we applied the Reader role. This role is one of many built-in roles and might include more permissions than you require. More granular roles and permissions can be created. For more information, see [Manage access to Log Analytics workspaces](../../logs/manage-access.md).
98
105
99
-
## Request an Authorization Token
106
+
## Request an authorization token
100
107
101
-
Before beginning, make sure you have all the values required to make the request successfully. All requests require:
102
-
- Your Azure Active Directory tenant ID.
108
+
Before you begin, make sure you have all the values required to make the request successfully. All requests require:
109
+
- Your Azure AD tenant ID.
103
110
- Your workspace ID.
104
-
- Your Azure Active Directory client ID for the app.
105
-
- An Azure Active Directory client secret for the app.
111
+
- Your Azure AD client ID for the app.
112
+
- An Azure AD client secret for the app.
106
113
107
-
The Log Analytics API supports Azure Active Directory authentication with three different [Azure AD OAuth2](/azure/active-directory/develop/active-directory-protocols-oauth-code) flows:
108
-
- Client credentials
114
+
The Log Analytics API supports Azure AD authentication with three different [Azure AD OAuth2](/azure/active-directory/develop/active-directory-protocols-oauth-code) flows:
115
+
- Client credentials
109
116
- Authorization code
110
117
- Implicit
111
118
119
+
### Client credentials flow
112
120
113
-
### Client Credentials Flow
121
+
In the client credentials flow, the token is used with the Log Analytics endpoint. A single request is made to receive a token by using the credentials provided for your app in the previous step when you [register an app in Azure AD](./register-app-for-token.md).
114
122
115
-
In the client credentials flow, the token is used with the log analytics endpoint. A single request is made to receive a token, using the credentials provided for your app in the [Register an app for in Azure Active Directory](./register-app-for-token.md) step above.
116
-
Use the `https://api.loganalytics.azure.com` endpoint.
123
+
Use the `https://api.loganalytics.azure.com` endpoint.
117
124
118
-
#####Client Credentials Token URL (POST request)
125
+
#### Client credentials token URL (POST request)
119
126
120
127
```http
121
128
POST /<your-tenant-id>/oauth2/token
@@ -139,7 +146,7 @@ A successful request receives an access token in the response:
139
146
}
140
147
```
141
148
142
-
Use the token in requests to the log analytics endpoint:
149
+
Use the token in requests to the Log Analytics endpoint:
143
150
144
151
```http
145
152
POST /v1/workspaces/your workspace id/query?timespan=P1D
@@ -153,8 +160,7 @@ Use the token in requests to the log analytics endpoint:
153
160
}
154
161
```
155
162
156
-
157
-
Example Response:
163
+
Example response:
158
164
159
165
```http
160
166
{
@@ -190,11 +196,11 @@ Example Response:
190
196
}
191
197
```
192
198
193
-
### Authorization Code Flow
199
+
### Authorization code flow
194
200
195
-
The main OAuth2 flow supported is through [authorization codes](/azure/active-directory/develop/active-directory-protocols-oauth-code). This method requires two HTTP requests to acquire a token with which to call the Azure Monitor Log Analytics API. There are two URLs, one endpoint per request. Their formats are:
201
+
The main OAuth2 flow supported is through [authorization codes](/azure/active-directory/develop/active-directory-protocols-oauth-code). This method requires two HTTP requests to acquire a token with which to call the Azure Monitor Log Analytics API. There are two URLs, with one endpoint per request. Their formats are described in the following sections.
196
202
197
-
#### Authorization Code URL (GET request):
203
+
#### Authorization code URL (GET request)
198
204
199
205
```http
200
206
GET https://login.microsoftonline.com/YOUR_Azure AD_TENANT/oauth2/authorize?
@@ -204,15 +210,15 @@ The main OAuth2 flow supported is through [authorization codes](/azure/active-di
204
210
&resource=https://api.loganalytics.io
205
211
```
206
212
207
-
When making a request to the Authorize URL, the client\_id is the Application ID from your Azure AD App, copied from the App's properties menu. The redirect\_uri is the home page/login URL from the same Azure AD App. When a request is successful, this endpoint redirects you to the sign-in page you provided at sign-up with the authorization code appended to the URL. See the following example:
213
+
When a request is made to the authorize URL, the client\_id is the application ID from your Azure AD app, copied from the app's properties menu. The redirect\_uri is the homepage/login URL from the same Azure AD app. When a request is successful, this endpoint redirects you to the sign-in page you provided at sign-up with the authorization code appended to the URL. See the following example:
At this point you'll have obtained an authorization code, which you need now to request an access token.
219
+
At this point, you've obtained an authorization code, which you need now to request an access token.
214
220
215
-
#### Authorization Code Token URL (POST request)
221
+
#### Authorization code token URL (POST request)
216
222
217
223
```http
218
224
POST /YOUR_Azure AD_TENANT/oauth2/token HTTP/1.1
@@ -227,7 +233,7 @@ At this point you'll have obtained an authorization code, which you need now to
227
233
&client_secret=<app-client-secret>
228
234
```
229
235
230
-
All values are the same as before, with some additions. The authorization code is the same code you received in the previous request after a successful redirect. The code is combined with the key obtained from the Azure AD App. If you didn't save the key, you can delete it and create a new one from the keys tab of the Azure AD App menu. The response is a JSON string containing the token with the following schema. Types are indicated for the token values.
236
+
All values are the same as before, with some additions. The authorization code is the same code you received in the previous request after a successful redirect. The code is combined with the key obtained from the Azure AD app. If you didn't save the key, you can delete it and create a new one from the keys tab of the Azure AD app menu. The response is a JSON string that contains the token with the following schema. Types are indicated for the token values.
231
237
232
238
Response example:
233
239
@@ -245,7 +251,7 @@ Response example:
245
251
}
246
252
```
247
253
248
-
The access token portion of this response is what you present to the Log Analytics API in the `Authorization: Bearer` header. You may also use the refresh token in the future to acquire a new access\_token and refresh\_token when yours have gone stale. For this request, the format and endpoint are:
254
+
The access token portion of this response is what you present to the Log Analytics API in the `Authorization: Bearer` header. You can also use the refresh token in the future to acquire a new access\_token and refresh\_token when yours have gone stale. For this request, the format and endpoint are:
249
255
250
256
```http
251
257
POST /YOUR_AAD_TENANT/oauth2/token HTTP/1.1
@@ -272,11 +278,11 @@ Response example:
272
278
}
273
279
```
274
280
275
-
### Implicit Code Flow
281
+
### Implicit code flow
276
282
277
-
The Log Analytics API supports the OAuth2 [implicit flow](/azure/active-directory/develop/active-directory-dev-understanding-oauth2-implicit-grant). For this flow, only a single request is required but no refresh token can be acquired.
283
+
The Log Analytics API supports the OAuth2 [implicit flow](/azure/active-directory/develop/active-directory-dev-understanding-oauth2-implicit-grant). For this flow, only a single request is required, but no refresh token can be acquired.
278
284
279
-
#### Implicit Code Authorize URL
285
+
#### Implicit code authorize URL
280
286
281
287
```http
282
288
GET https://login.microsoftonline.com/YOUR_AAD_TENANT/oauth2/authorize?
@@ -286,25 +292,24 @@ The Log Analytics API supports the OAuth2 [implicit flow](/azure/active-director
286
292
&resource=https://api.loganalytics.io
287
293
```
288
294
289
-
A successful request will produce a redirect to your redirect URI with the token in the URL as follows.
295
+
A successful request produces a redirect to your redirect URI with the token in the URL:
0 commit comments