Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Apr 13, 2021 9:53:53 AM
Apr 13, 2021 9:53:53 AM
This is a sample of how to call the NPrinting API to list users with PowerShell/Windows authentication.
In order to call NPrinting API with Windows authentication, the first step is to call GET api/v1/login/ntlm to get a cookie, you will need to be logged in as a domain user that has proper privileges in NPrinting to run the following PowerShell script.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $hdrs = @{} $url = "https://servername:4993/api/v1/login/ntlm" $resp = Invoke-WebRequest -Uri $url -Method Get -ContentType 'application/json' -Headers $hdrs -UseDefaultCredentials $substring= [Regex]::Matches($resp.RawContent, "(?<=SameSite=None,NPWEBCONSOLE_SESSION\=).+?(?=; Path)") $substring2= [Regex]::Matches($resp.RawContent, "(?<=NPWEBCONSOLE_XSRF-TOKEN\=).+?(?=; Path)") #Create session $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession $cookie = New-Object System.Net.Cookie $cookie.Name = "NPWEBCONSOLE_SESSION" $cookie.Value = $substring.value $cookie.Domain = "servername" $session.Cookies.Add($cookie); $hdrs.add("X-XSRF-TOKEN",$substring2.value) $url = "https://servername:4993/api/v1/users" Invoke-RestMethod -Uri $url -Method Get -ContentType 'application/json' -Headers $hdrs -WebSession $session
For Nprinting versions prior to April 2020 Patch 1, the following line:
$substring= [Regex]::Matches($resp.RawContent, "(?<=SameSite=None,NPWEBCONSOLE_SESSION\=).+?(?=; Path)")
will need to be:
$substring= [Regex]::Matches($resp.RawContent, "(?<=Secure,NPWEBCONSOLE_SESSION\=).+?(?=; Path)")
Regarding CSRF protection:
CSRF protection is needed on POST/PUT/PATCH calls (considered calls that modify data). In the sample above, that is done with the following 2 lines:
$substring2= [Regex]::Matches($resp.RawContent, "(?<=NPWEBCONSOLE_XSRF-TOKEN\=).+?(?=; Path)") .... $hdrs.add("X-XSRF-TOKEN",$substring2.value)
As an alternative, the "Origin" header can also be used instead of the X-XSRF-TOKEN header, for example:
$hdrs.add("Origin","https://qlikserver2.domain.local")
The origin needs to be added in Trusted Origins in the "Settings" > "On-Demand" section of the NPrinting console.