Last active
December 18, 2018 14:34
-
-
Save praveenc/5a160435642b5c953f5cfc25d06ac0dc to your computer and use it in GitHub Desktop.
Following is a sample header for authenticating with Azure. Generate an authentication header, sometimes called a Bearer token, and provide the REST API URI to connect to with any parameters or a Request Body
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Login first with Connect-AzureRmAccount if not using Cloud Shell | |
| $azContext = Get-AzureRmContext | |
| $azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile | |
| $profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile) | |
| $token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId) | |
| $authHeader = @{ | |
| 'Content-Type'='application/json' | |
| 'Authorization'='Bearer ' + $token.AccessToken | |
| } | |
| $authHeader | |
| # Invoke the REST API | |
| # $restUri = 'https://management.azure.com/subscriptions/{subscriptionId}?api-version=2016-06-01' | |
| # $response = Invoke-RestMethod -Uri $restUri -Method Get -Headers $authHeader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment