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