Skip to content

Instantly share code, notes, and snippets.

@nimdaus
Created January 21, 2025 19:50
Show Gist options
  • Select an option

  • Save nimdaus/69c0f4a8ff219fd3fbd7977429635281 to your computer and use it in GitHub Desktop.

Select an option

Save nimdaus/69c0f4a8ff219fd3fbd7977429635281 to your computer and use it in GitHub Desktop.
$region = Read-Host "Enter a region, e.g., CA"
$client_id = Read-Host "Enter the client_id"
$client_secret = Read-Host "Enter the client_secret" -AsSecureString
$response_type = "code"
$scope = Read-Host "Enter *SPACE* separated scope, e.g., control management monitoring"
$scope = [uri]::EscapeDataString($scope) #this encoded right
$plain_client_secret = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($client_secret))
$redirect_uri = Read-Host "Enter the route from app registration, e.g., https://localhost *OR* https://webhook.site/00000000-0000-0000-0000-000000000000"
$uri = "https://$($region).ninjarmm.com/oauth/authorize?" +
"response_type=$($response_type)&" +
"client_id=$($client_id)&" +
"client_secret=$($plain_client_secret)&" +
"redirect_uri=$($redirect_uri)&" +
"scope=$scope"
Start-Process $uri
$auth_code = Read-Host "Enter the authorization code from receipt" -AsSecureString
$plain_auth_code = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($auth_code))
$headers = @{
"Content-Type" = "application/x-www-form-urlencoded"
}
$grant_type = "authorization_code"
$body = "grant_type=$($grant_type)&" +
"client_id=$($client_id)&" +
"client_secret=$($plain_client_secret)&" +
"code=$($plain_auth_code)&" +
"redirect_uri=$($redirect_uri)"
$response = Invoke-WebRequest -Uri "https://$($region).ninjarmm.com/ws/oauth/token" -Method POST -Headers $headers -Body $body
$response_data = $response.Content | ConvertFrom-Json
$access_token = $response_data.access_token
$headers = @{
"Accept" = "application/json"
"Authorization" = "Bearer $access_token"
}
$response = Invoke-WebRequest -Uri "https://$($region).ninjarmm.com/v2/devices" -Method GET -Headers $headers
$response | ConvertFrom-Json | ConvertTo-Json -Depth 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment