Already a NinjaOne customer? Log in to view more guides and the latest updates.

Automate the NinjaRMM Authorization Flow with PowerShell

NinjaDojo-logo-darkblue.png

Users can automate the authorization process while using the NinjaRMM API from here, https://oc.ninjarmm.com/apidocs-beta/authorization/overview.

The PowerShell module, PSAuthClient (https://github.com/alflokken/PSAuthClient),will help with the automation.

Use the following code to get started. Initially, you will need to create yourself an API token in the portal. Make sure you note the redirect URI and scope and update the variables below to reflect them. Note the last forward slash '/' at the end of the localhost address.

You will also need to update the ninja URI so it matches your region.

Install-Module PSAuthClient -Confirm:$false
Import-Module PSAuthClient

$clientId     = ''
$clientSecret = ''
$redirectUri  = 'https://localhost/'
$scope        = 'monitoring offline_access'

$custom = @{ 
    "client_secret" = $clientSecret 
}
$authParams = @{
    Uri              = "https://oc.ninjarmm.com/ws/oauth/authorize"
    Redirect_uri     = $redirectUri
    Client_id        = $clientId
    Scope            = $scope
    UsePkce          = $false
    CustomParameters = $custom
}
$auth = Invoke-OAuth2AuthorizationEndpoint @authParams -verbose

$authCode = $auth.code
$tokenParams = @{
    uri           = 'https://oc.ninjarmm.com/ws/oauth/token' 
    redirect_uri  = $redirectUri
    client_secret = $clientSecret
    code          = $authCode
    client_id     = $clientId
}
$token = Invoke-OAuth2TokenEndpoint @tokenParams

FAQ

Next Steps