Skip to content

Instantly share code, notes, and snippets.

@buchizo
Created February 29, 2016 04:09
Show Gist options
  • Save buchizo/3346e4188ea9ff59851b to your computer and use it in GitHub Desktop.
Save buchizo/3346e4188ea9ff59851b to your computer and use it in GitHub Desktop.
get-bearertoken from Azure using AzurePowerShell
$adal = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
[System.Reflection.Assembly]::LoadFrom($adal)
[System.Reflection.Assembly]::LoadFrom($adalforms)
$adTenant = "buchizomsdn.onmicrosoft.com"
# well-known ID
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2"
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$resourceAppIdURI = "https://management.core.windows.net/"
$authority = "https://login.windows.net/$adTenant"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
$authHeader = $authResult.CreateAuthorizationHeader()
# Write-Host "Bearer Token: $authHeader"
$authHeader | Out-File bearerToken.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment