Skip to content

Instantly share code, notes, and snippets.

@praveenc
Last active December 18, 2018 14:34
Show Gist options
  • Save praveenc/5a160435642b5c953f5cfc25d06ac0dc to your computer and use it in GitHub Desktop.
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
# 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