Skip to content

Instantly share code, notes, and snippets.

@markarnott
Last active September 21, 2022 00:22
Show Gist options
  • Save markarnott/95cfafbc7ac475930a95ebb72b19c3bf to your computer and use it in GitHub Desktop.
Save markarnott/95cfafbc7ac475930a95ebb72b19c3bf to your computer and use it in GitHub Desktop.
Get an Access Token for Azure
#!/bin/bash
# ==== Using the Az CLI ====
az login
accessToken=$(az account get-access-token --query accessToken | tr -d \")
# ==== Using the Az CLI ====
az login
$accessToken = (az account get-access-token --query accessToken).Trim('"')
# ==== Using PwSH Az Modules ====
Import-Module Az.Accounts
Connect-AzAccount
$accessToken = (Get-AzAccessToken).Token
$clientId = "6731de76-14a6-49ae-97bc-6eba6914391e"
$Resp = (curl -X POST `
-d client_id=$clientID `
-d 'scope=user.read%20openid%20profile' `
https://login.microsoftonline.com/common/oauth2/v2.0/devicecode
) | ConvertFrom-Json
Start-Process $Resp.verification_uri
$devCodeData = "device_code=$($Resp.device_code)"
$Resp2 = (curl -X POST `
-d grant_type=urn:ietf:params:oauth:grant-type:device_code `
-d $devCodeData `
-d client_id=$clientID `
https://login.microsoftonline.com/common/oauth2/v2.0/token
) | ConvertFrom-Json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment