Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WimObiwan/c2b3472e20d84438856baacada6845aa to your computer and use it in GitHub Desktop.
Save WimObiwan/c2b3472e20d84438856baacada6845aa to your computer and use it in GitHub Desktop.
function Get-AccessTokenClientCredentialsFlow {
param (
[string]$ClientId,
[string]$ClientSecret,
[string]$Audience
)
Add-Type -AssemblyName System.Web
$clientidEnc = [System.Web.HttpUtility]::UrlEncode($ClientId)
$clientsecretEnc = [System.Web.HttpUtility]::UrlEncode($ClientSecret)
$audienceEnc = [System.Web.HttpUtility]::UrlEncode($Audience)
$auth = Invoke-RestMethod -Method POST -Uri 'https://organimmo.eu.auth0.com/oauth/token' `
-Headers @{ 'content-type' = 'application/x-www-form-urlencoded' } `
-Body (
"grant_type=client_credentials" +
"&client_id=$clientidEnc" +
"&client_secret=$clientsecretEnc" +
"&audience=$audienceEnc"
)
return $auth.access_token
}
# Get client credentials from Windows Crendential Manager
$cred = Get-StoredCredential -Target 'Auth0-API-key'
$clientId = $cred.Username
$clientSecret = $cred.GetNetworkCredential().Password
# Get access token
$audience = 'https://organimmo.eu.auth0.com/api/v2/'
$accesstoken = & Get-AccessTokenClientCredentialsFlow -ClientId $clientId -ClientSecret $clientSecret -Audience $audience
$date_cutoff = (Get-Date).AddDays(-30).ToString('yyyy-MM-dd')
$logQuery = "last_login:[* TO $date_cutoff]"
$result = Invoke-RestMethod -Method GET -Headers @{ 'Authorization' = "Bearer $accesstoken" } `
-Uri ('https://organimmo.eu.auth0.com/api/v2/users?q=' + [System.Web.HttpUtility]::UrlEncode($logQuery) +
'&per_page=100')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment