Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Jul 3, 2024 1:38:01 AM
Jul 2, 2024 11:23:35 PM
This is a sample of how to call the Qlik Cloud APIs to assign an Analyzer license to a user with PowerShell/JWT authentication.
In order to call Qlik Cloud API with JWT authentication, the first step is to call POST /login/jwt-session to get the necessary cookies. Which API can be called depends on the privileges the JWT user has been assigned.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$hdrs = @{}
# Put your JWT token here
$hdrs.Add("Authorization","Bearer eyJhbGciOiJSU...pgacN8QqAjKug")
$url = "https://tenant.ap.qlikcloud.com/login/jwt-session"
$resp = Invoke-WebRequest -Uri $url -Method Post -ContentType 'application/json' -Headers $hdrs
#Fetch all required cookies
$AWSALB= [Regex]::Matches($resp.RawContent, "(?<=AWSALB\=).+?(?=; Expires)")
$AWSALBCORS= [Regex]::Matches($resp.RawContent, "(?<=AWSALBCORS\=).+?(?=; Expires)")
$eassid= [Regex]::Matches($resp.RawContent, "(?<=eas.sid\=).+?(?=; path)")
$eassidsig= [Regex]::Matches($resp.RawContent, "(?<=eas.sid.sig\=).+?(?=; path)")
$csrftoken= [Regex]::Matches($resp.RawContent, "(?<=_csrfToken\=).+?(?=; path)")
$csrftokensig= [Regex]::Matches($resp.RawContent, "(?<=_csrfToken.sig\=).+?(?=; path)")
$allCookies = "AWSALB="+$AWSALB.Value+";AWSALBCORS="+$AWSALBCORS.Value+";eas.sid="+$eassid.Value+";eas.sid.sig="+$eassidsig.Value+";_csrfToken="+$csrftoken.Value+";_csrfToken.sig="+$csrftokensig.Value
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
foreach ($cookiePair in $allCookies.Split((";"))) {
$cookieValues = $cookiePair.Trim().Split("=")
$cookie = New-Object System.Net.Cookie
$cookie.Name = $cookieValues[0]
$cookie.Value = $cookieValues[1]
$cookie.Domain = "tenant.ap.qlikcloud.com"
$session.Cookies.Add($cookie);
}
$hdrs = @{}
#Request that modify content such as POST/PATCH requests need the Qlik-Csrf-Token header
$hdrs.Add("Qlik-Csrf-Token",$csrftoken.value)
$body = '{"add":[{"subject":"DOMAIN\\user1","type":"analyzer"}]}'
$url = "https://tenant.ap.qlikcloud.com/api/v1/licenses/assignments/actions/add"
Invoke-RestMethod -Uri $url -Method Post -Headers $hdrs -Body $body -WebSession $session