Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
amalsyed93
Contributor
Contributor

QRS API with Powershell and Domain Creds

Below is the code snippet from QRS API Documentation for powershell

 

$hdrs = @{}
$hdrs.Add("X-Qlik-xrfkey","12345678qwertyui")
$url = "https://servername/qrs/about?xrfkey=12345678qwertyui"
Invoke-RestMethod -Uri $url -Method Get -Headers $hdrs -UseDefaultCredentials

 

 

How do we make this work with another set of domain credentials rather than using Default Credentials or Certificate? I tired the -cred parameter, wrapping them in to basic auth header, nothing works unforunately.

Anybody here has an example or suggestions?

 

Thanks! 

Labels (4)
1 Reply
Marc
Employee
Employee

Invoke-RestMethod automatically follows redirection.

however, it does not pass the supplied credentials to the redirected URL. 

In order to get around this, we need to break the automatic redirection, identify the redirection URI and authenticate against that.

$cred = Get-Credential 

$url = "https://servername/qrs/about?xrfkey=12345678qwertyui"

$Result = Invoke-WebRequest -Method Get -Uri $url -MaximumRedirection 0 -ErrorAction SilentlyContinue -Credential $cred -Headers $hdrs   

#Get the Auth URL
$AuthURL = $Result.Headers.Location

#Authenticate to the AuthURL, and create a SessionVariable 
$Results = Invoke-RestMethod -Method Get -Uri $AuthURL -ErrorAction SilentlyContinue -Credential $cred -Headers $hdrs -SessionVariable AuthenticatedSession

#Subsequent requests can use the Session variable
Invoke-RestMethod -WebSession $AuthenticatedSession -Uri $URL