|
| 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 | +--- |
0 commit comments