Qlik Community

New to Qlik Sense

If you’re new to Qlik Sense, start with this Discussion Board and get up-to-speed quickly.

Announcements
QlikWorld 2022, LIVE in Denver CO., May 16-19, 2022. REGISTER NOW TO RECEIVE EARLY BIRD PRICING
cancel
Showing results for 
Search instead for 
Did you mean: 
rthamman
Contributor III
Contributor III

Java sample

Wondering if anyone can post a java sample for getting an access ticket from Qlik Sense. I did see a .NET example in the documentation but a Java sample will help.

3 Replies
Not applicable

I am also looking for similar examples. Let us know if you have any samples on java to generate / get tickets.

rthamman
Contributor III
Contributor III
Author

Here is a sample but QlikSense is returning an error "XSRF prevention check failed. Possible XSRF discovered."

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URL;

import java.security.KeyStore;

import java.security.SecureRandom;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.HttpsURLConnection;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.SSLSocketFactory;

public class QlikTicket {

   

    static {

   

        //if certificate for create for an IP address you need to do this.

        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()

            {

                public boolean verify(String hostname, SSLSession session)               

                {

                    if (hostname.equals("xx.xx.xx.xx"))

                        return true;

                    return false;

                }

            });

    }

    public static void main(String args[]) {

        HttpsURLConnection request = null;

        InputStream inputStream = null;

   

        try {

            KeyStore keyStore  = KeyStore.getInstance(KeyStore.getDefaultType());

            FileInputStream instream = new FileInputStream(new File("path to jks that has the client cert"));

            keyStore.load(instream, "password".toCharArray());

            instream.close();

           

            String Xrfkey = "0123456789abcdef";

            String targetURL = "https://xx.xx.xx.xx:4243/qps/ticket?Xrfkey=";

           

            URL url = new URL(targetURL + "?Xrfkey=" + Xrfkey);

            request = (HttpsURLConnection)url.openConnection();

            request.setRequestMethod("POST");

            request.setRequestProperty("Content-Type", "application/json");

            request.setRequestProperty("X-Qlik-Xrfkey", Xrfkey);

            request.setUseCaches(false);

            request.setDoInput(true);

            request.setDoOutput(true);

           

            String userDirectory = "USERDIRECTORY";

            String userId= "USERID";

            String body = "{'UserDirectory':'" + userDirectory + "', 'UserId':'" + userId + "','Attributes': []}";

            byte[] bodyBytes = body.getBytes("UTF-8");

           

            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");

            keyManagerFactory.init(keyStore, "password".toCharArray());

            SSLContext sslContext = SSLContext.getInstance("TLS");

            sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            request.setSSLSocketFactory(sslSocketFactory);

           

            request.setDoOutput(true);

            request.setDoInput(true);

            DataOutputStream out = new DataOutputStream(request.getOutputStream());

            out.write(bodyBytes);

            out.flush();

            out.close();

            inputStream = request.getErrorStream();

            InputStreamReader inputStreamReader = null;

            String string = null;

            BufferedReader bufferedreader = null;

            if(inputStream != null) {

                inputStreamReader = new InputStreamReader(inputStream);

                bufferedreader = new BufferedReader(inputStreamReader);

                string = null;

                while ((string = bufferedreader.readLine()) != null) {

                    System.out.println("Error Received " + string);

                }

            }

           

            inputStream = request.getInputStream();

            inputStreamReader = new InputStreamReader(inputStream);

            bufferedreader = new BufferedReader(inputStreamReader);

            while ((string = bufferedreader.readLine()) != null) {

                System.out.println("Received " + string);

            }

           

            inputStream = request.getInputStream();

            inputStreamReader = new InputStreamReader(inputStream);

            bufferedreader = new BufferedReader(inputStreamReader);

            while ((string = bufferedreader.readLine()) != null) {

                System.out.println("Received " + string);

            }

        }catch (Exception ex) {

            ex.printStackTrace();

            if(inputStream != null) {

                try {

                    inputStream.close();

                } catch(Exception ex1) {

                   

                }

            }

        }

    }       

}

Error:

00C0: 46 72 69 2C 20 31 30 20   41 70 72 20 32 30 31 35  Fri, 10 Apr 2015

00D0: 20 30 36 3A 34 38 3A 31   30 20 47 4D 54 0D 0A 0D   06:48:10 GMT...

00E0: 0A 33 37 0D 0A 58 53 52   46 20 70 72 65 76 65 6E  .37..XSRF preven

00F0: 74 69 6F 6E 20 63 68 65   63 6B 20 66 61 69 6C 65  tion check faile

0100: 64 2E 20 50 6F 73 73 69   62 6C 65 20 58 53 52 46  d. Possible XSRF

0110: 20 64 69 73 63 6F 76 65   72 65 64 2E 0D 0A 7F 6E   discovered....n

0120: 8B 7B 18 15 D4 FC 0A 2C   9F 13 7E 96 27 99 3E 6B  .......,....'.>k

0130: C1 EB 0D 0D 0D 0D 0D 0D   0D 0D 0D 0D 0D 0D 0D 0D  ................

StevenJDH
Employee
Employee

Hello all,

For those who are interested, I've created an updated example for how to request a ticket via Java. This version is more adaptable with a number of improvements and has better clarity. The biggest change is that you can now also use the standard certificates that are exported from Qlik Sense without having to convert them to the Java KeyStore (*.jks) format. You can find the code here: https://github.com/StevenJDH/Qlik-Sense-Java-Examples