Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW

How to Login / Authenticate / Connect to the NPrinting APIs in Powershell

No ratings
cancel
Showing results for 
Search instead for 
Did you mean: 
Sonja_Bauernfeind
Digital Support
Digital Support

How to Login / Authenticate / Connect to the NPrinting APIs in Powershell

Last Update:

Jun 1, 2021 2:59:54 PM

Updated By:

Andre_Sostizzo

Created date:

Jun 21, 2018 8:29:19 AM


How can one connect to the Nprinting API via Powershell, such as in cases when using a REST client such as Postman isn't possible?

Environments:

 

Resolution:


Connecting to the NPrinting API is documented on our help site, in the section Extending Qlik Nprinting.

Connecting to the Nprinting API is done in the following steps:

  1. Install Nprinting certificate as Trusted Root CA on machine where you will execute the tasks (so that Powershell client trusts the certificate)
    1. Open URL to web console (https://YOURSERVER:4993) in Internet Explorer
    2. Click the lock icon next to the URL > View Certificates
    3. Click General > Install Certificate…
    4. Follow the wizard, make sure to manually specify the installation location as the Trusted Root Certificate Authorities folder
  2. Authenticate via NTLM credentials at /api/v1/login/ntlm at port 4993.
  3. Save the Session and XSRF cookies somewhere.
  4. Make subsequent requests using the cookies.
Note: POST requests require the X-XSRF-token header (with value from the XSRF cookie) is required.
Note: Powershell defaults to TLS 1.0, and the April 2018 release uses TLS 1.1 minimum. You must force Powershell to use at least this version of TLS in April 2018 and higher.


Here is some example code that (1) Logs in to Nprinting as the user executing the script, (2) makes a GET request to retrieve the first task, and (3) makes a POST request to execute said task.

# Set TLS to minimum 1.1 for Nprinting Feb 2018, using 1.2 in this example
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Authenticate and get cookie
$url = "https://YOUR-NPRINTING-SERVER:4993/api/v1/login/ntlm"
Invoke-RestMethod -UseDefaultCredentials -Uri $url -Method Get -Headers $hdrs -SessionVariable websession
$cookies = $websession.Cookies.GetCookies($url)
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.Add($cookies);

# Extract XSRF token from cookie
$xsrf_token = $($cookies | Where-Object {$_.Name -eq "NPWEBCONSOLE_XSRF-TOKEN"}).Value

# Get list of tasks in Nprinting
$hdrs = @{}
$hdrs.Add("X-XSRF-token",$xsrf_token)
$url = "https://YOUR-NPRINTING-SERVER:4993/api/v1/tasks"
$tasks = $(Invoke-RestMethod -WebSession $session -Uri $url -Method Get -Headers $hdrs).data.items

# Pick an arbitrary task (first one) and execute it
$taskid = $tasks[0].id 
$url = "https://YOUR-NPRINTING-SERVER:4993/api/v1/tasks/$($taskid)/executions"
Invoke-RestMethod -WebSession $session -Uri $url -Method Post -Headers $hdrs


Note: Debugging or writing custom code is supported by the Qlik Professional Services or Presales teams. This example is provided for demonstration purposes to explain specific scenarios. No Support or maintenance is implied or provided. Further customization is expected to be necessary and it is the responsibility of the end administrator to test and implement an appropriate implementation for their specific use case.

Labels (2)
Contributors
Version history
Last update:
‎2021-06-01 02:59 PM
Updated by: