2 Replies Latest reply: Apr 10, 2015 2:54 AM by Rama Thamman RSS

    Java sample

    Rama Thamman

      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.

        • Re: Java sample

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

            • Re: Java sample
              Rama Thamman

              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  ................