Skip to content

Commit 62eabc5

Browse files
authored
Merge pull request #279751 from EdB-MSFT/get-tokens
get token include
2 parents bc984dd + 1c236b6 commit 62eabc5

File tree

4 files changed

+171
-82
lines changed

4 files changed

+171
-82
lines changed

articles/azure-monitor/essentials/metrics-store-custom-rest-api.md

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,10 @@ To submit custom metrics to Azure Monitor, the entity that submits the metric ne
4444
4545
### Get an authorization token
4646

47-
Once you have created your managed identity or service principal and assigned **Monitoring Metrics Publisher** permissions, you can get an authorization token by using the following request:
48-
49-
```console
50-
curl -X POST 'https://login.microsoftonline.com/<tennant ID>/oauth2/token' \
51-
-H 'Content-Type: application/x-www-form-urlencoded' \
52-
--data-urlencode 'grant_type=client_credentials' \
53-
--data-urlencode 'client_id=<your apps client ID>' \
54-
--data-urlencode 'client_secret=<your apps client secret' \
55-
--data-urlencode 'resource=https://monitoring.azure.com'
56-
```
57-
58-
The response body appears in the following format:
47+
Once you have created your managed identity or service principal and assigned **Monitoring Metrics Publisher** permissions, you can get an authorization token.
48+
When requesting a token specify `resource: https://monitoring.azure.com`.
5949

60-
```JSON
61-
{
62-
"token_type": "Bearer",
63-
"expires_in": "86399",
64-
"ext_expires_in": "86399",
65-
"expires_on": "1672826207",
66-
"not_before": "1672739507",
67-
"resource": "https://monitoring.azure.com",
68-
"access_token": "eyJ0eXAiOiJKV1Qi....gpHWoRzeDdVQd2OE3dNsLIvUIxQ"
69-
}
70-
```
50+
[!INCLUDE [Get a token](../includes/get-authentication-token.md)]
7151

7252
Save the access token from the response for use in the following HTTP requests.
7353

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

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,9 @@ Retrieve metric definitions, dimension values, and metric values using the Azure
2020
Request submitted using the Azure Monitor API use the Azure Resource Manager authentication model. All requests are authenticated with Microsoft Entra ID. One approach to authenticating the client application is to create a Microsoft Entra service principal and retrieve an authentication token. You can create a Microsoft Entra service principal using the Azure portal, CLI, or PowerShell. For more information, see [Register an App to request authorization tokens and work with APIs](../logs/api/register-app-for-token.md).
2121

2222
### Retrieve a token
23-
Once you've created a service principal, retrieve an access token using a REST call. Submit the following request using the `appId` and `password` for your service principal or app:
24-
25-
```HTTP
26-
27-
POST /<tenant-id>/oauth2/token
28-
Host: https://login.microsoftonline.com
29-
Content-Type: application/x-www-form-urlencoded
30-
31-
grant_type=client_credentials
32-
&client_id=<app-client-id>
33-
&resource=https://management.azure.com
34-
&client_secret=<password>
35-
36-
```
37-
38-
For example
39-
40-
```bash
41-
curl --location --request POST 'https://login.microsoftonline.com/abcd1234-5849-4a5d-a2eb-5267eae1bbc7/oauth2/token' \
42-
--header 'Content-Type: application/x-www-form-urlencoded' \
43-
--data-urlencode 'grant_type=client_credentials' \
44-
--data-urlencode 'client_id=0123b56a-c987-1234-abcd-1a2b3c4d5e6f' \
45-
--data-urlencode 'client_secret=123456.ABCDE.~XYZ876123ABceDb0000' \
46-
--data-urlencode 'resource=https://management.azure.com'
47-
48-
```
49-
A successful request receives an access token in the response:
50-
51-
```HTTP
52-
{
53-
token_type": "Bearer",
54-
"expires_in": "86399",
55-
"ext_expires_in": "86399",
56-
"access_token": "eyJ0eXAiOiJKV1QiLCJ.....Ax"
57-
}
58-
```
23+
Once you've created a service principal, retrieve an access token. Specify `resource=https://management.azure.com` in the token request.
5924

25+
[!INCLUDE [Get a token](../includes/get-authentication-token.md)]
6026

6127

6228
After authenticating and retrieving a token, use the access token in your Azure Monitor API requests by including the header `'Authorization: Bearer <access token>'`
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
ms.service: azure-monitor
3+
ms.topic: include
4+
ms.date: 07/01/2024
5+
ms.author: edbaynash
6+
author: EdB-MSFT
7+
---
8+
9+
Get an authentication token using any of the following methods:
10+
- CLI
11+
- REST API
12+
- SDK
13+
14+
When requesting a token, you must provide a `resource` parameter. The `resource` parameter is the URL of the resource you want to access.
15+
16+
Resources include:
17+
- https://management.azure.com
18+
- https://api.loganalytics.io
19+
- https://monitoring.azure.com
20+
21+
22+
## [REST](#tab/rest)
23+
### Get a token using a REST request
24+
Use the following REST API call to get a token.
25+
This request uses a client ID and client secret to authenticate the request. The client ID and client secret are obtained when you register your application with Microsoft Entra ID. For more information, see [Register an App to request authorization tokens and work with APIs](/azure/azure-monitor/logs/api/register-app-for-token?tabs=portal)
26+
27+
28+
```console
29+
curl -X POST 'https://login.microsoftonline.com/<tennant ID>/oauth2/token' \
30+
-H 'Content-Type: application/x-www-form-urlencoded' \
31+
--data-urlencode 'grant_type=client_credentials' \
32+
--data-urlencode 'client_id=<your apps client ID>' \
33+
--data-urlencode 'client_secret=<your apps client secret' \
34+
--data-urlencode 'resource=https://monitoring.azure.com'
35+
```
36+
37+
The response body appears in the following format:
38+
39+
```JSON
40+
{
41+
"token_type": "Bearer",
42+
"expires_in": "86399",
43+
"ext_expires_in": "86399",
44+
"expires_on": "1672826207",
45+
"not_before": "1672739507",
46+
"resource": "https://monitoring.azure.com",
47+
"access_token": "eyJ0eXAiOiJKV1Qi....gpHWoRzeDdVQd2OE3dNsLIvUIxQ"
48+
}
49+
```
50+
51+
## [CLI](#tab/cli)
52+
### Get a token using Azure CLI
53+
To get a token using CLI, you can use the following command
54+
55+
```bash
56+
az account get-access-token
57+
```
58+
59+
For more information, see [az account get-access-token](/cli/azure/account?view=azure-cli-latest#az-account-get-access-token)
60+
61+
## [SDK](#tab/SDK)
62+
### Get a token using the SDKs
63+
The following code samples show how to get a token using:
64+
+ C#
65+
+ NodeJS
66+
+ Python
67+
68+
#### C#
69+
70+
The following code shows how to get a token using the Azure. Identity library It requires a client ID and client secret to authenticate the request.
71+
```csharp
72+
var context = new AuthenticationContext("https://login.microsoftonline.com/<tennant ID>");
73+
var clientCredential = new ClientCredential("<your apps client ID>", "<your apps client secret>");
74+
var result = context.AcquireTokenAsync("https://monitoring.azure.com", clientCredential).Result;
75+
```
76+
77+
Alternatively, you can use the DefaultAzureCredential class to get a token. This method uses the default Azure credentials to authenticate the request and doesn't require a client ID or client secret.
78+
79+
```csharp
80+
var credential = new DefaultAzureCredential();
81+
var token = credential.GetToken(new TokenRequestContext(new[] { "https://management.azure.com/.default" }));
82+
```
83+
84+
85+
You can also specify your managed identity or service principal credentials as follows:
86+
87+
```csharp
88+
string userAssignedClientId = "<your managed identity client ID>";
89+
var credential = new DefaultAzureCredential(
90+
new DefaultAzureCredentialOptions
91+
{
92+
ManagedIdentityClientId = userAssignedClientId
93+
});
94+
95+
var token = credential.GetToken(new TokenRequestContext(new[] { "https://management.azure.com/.default" }));
96+
97+
```
98+
For more information, see [DefaultAzureCredential Class](/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet)
99+
100+
101+
#### Node.js
102+
103+
For information on authentication use JavaScript and NodeJS, see [How to authenticate JavaScript apps to Azure services using the Azure SDK for JavaScript](/azure/developer/javascript/sdk/authentication/overview)
104+
105+
106+
The following code shows how to get a token using the DefaultAzureCredential class. This method uses the default Azure credentials to authenticate the request and doesn't require a client ID or client secret.
107+
108+
```javascript
109+
const { DefaultAzureCredential } = require("@azure/identity");
110+
111+
const credential = new DefaultAzureCredential();
112+
const accessToken = await credential.getToken("https://management.azure.com/.default");
113+
```
114+
115+
You can also use the `InteractiveBrowserCredential` class to get the credentials. This method provides a browser-based authentication experience for users to authenticate with Azure services.
116+
117+
For more information, see [DefaultAzureCredential Class](/javascript/api/@azure/identity/defaultazurecredential?view=azure-node-latest) and [InteractiveBrowserCredential Class](/javascript/api/@azure/identity/interactivebrowsercredential?view=azure-node-latest)
118+
119+
Alternatively you can use the ClientSecretCredential class to get a token. This method requires a client ID and client secret to authenticate the request.
120+
121+
```javascript
122+
const { ClientSecretCredential } = require("@azure/identity");
123+
credential = ClientSecretCredential(
124+
client_id="<client_id>",
125+
username="<username>",
126+
password="<password>"
127+
)
128+
const accessToken = await credential.getToken("https://management.azure.com/.default");
129+
```
130+
For more information, see [ClientSecretCredential Class](/javascript/api/@azure/identity/clientsecretcredential?view=azure-node-latest)
131+
132+
#### Python
133+
134+
The following code shows how to get a token using the DefaultAzureCredential class. This method uses the default Azure credentials to authenticate the request and doesn't require a client ID or client secret.
135+
136+
```python
137+
from azure.identity import DefaultAzureCredential
138+
139+
credential = DefaultAzureCredential()
140+
token = credential.get_token('https://management.azure.com/.default')
141+
print(token.token)
142+
```
143+
144+
You can also use the `InteractiveBrowserCredential` class to get the credentials. This method provides a browser-based authentication experience for users to authenticate with Azure services.
145+
146+
For more information, see [DefaultAzureCredential Class](/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python) and [InteractiveBrowserCredential Class](/python/api/azure-identity/azure.identity.interactivebrowsercredential?view=azure-python)
147+
148+
Alternatively you can use the ClientSecretCredential class to get a token. This method requires a client ID and client secret to authenticate the request.
149+
150+
```python
151+
from azure.identity import ClientSecretCredential
152+
153+
credential = ClientSecretCredential (
154+
tenant_id="<tenant id>",
155+
client_id="<Client id>",
156+
client_secret="client secret"
157+
)
158+
token = credential.get_token("https://management.azure.com/.default")
159+
print(token.token)
160+
```
161+
162+
For more information, see [ClientSecretCredential Class](/python/api/azure-identity/azure.identity.clientsecretcredential?view=azure-python)
163+
164+
---

articles/azure-monitor/logs/api/access-api.md

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,31 +120,10 @@ The Log Analytics API supports Microsoft Entra authentication with three differe
120120

121121
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 Microsoft Entra ID](./register-app-for-token.md).
122122

123-
Use the `https://api.loganalytics.azure.com` endpoint.
123+
Use `resource=https://api.loganalytics.azure.com`.
124124

125-
#### Client credentials token URL (POST request)
125+
[!INCLUDE [Get a token](../../includes/get-authentication-token.md)]
126126

127-
```http
128-
POST /<your-tenant-id>/oauth2/token
129-
Host: https://login.microsoftonline.com
130-
Content-Type: application/x-www-form-urlencoded
131-
132-
grant_type=client_credentials
133-
&client_id=<app-client-id>
134-
&resource=https://api.loganalytics.io
135-
&client_secret=<app-client-secret>
136-
```
137-
138-
A successful request receives an access token in the response:
139-
140-
```http
141-
{
142-
token_type": "Bearer",
143-
"expires_in": "86399",
144-
"ext_expires_in": "86399",
145-
"access_token": ""eyJ0eXAiOiJKV1QiLCJ.....Ax"
146-
}
147-
```
148127

149128
Use the token in requests to the Log Analytics endpoint:
150129

0 commit comments

Comments
 (0)