Qlik Community

Ask a Question

Qlik Sense Documents

Qlik Sense documentation and resources.

Announcements
Our next Qlik Insider session will cover new key capabilities. Join us August 11th REGISTER TODAY

How to connect python to Qlik sense server with certificates authentication?

bhargavpatel431
Contributor II
Contributor II

How to connect python to Qlik sense server with certificates authentication?

Python developer faced issue to connect qlik sense sever with websocket client. here is a tutorial to authenticate python to qlik sense server.

step 1:

Open QMC in qlik sense server.

step 2:

Go to certificates tab.

Screenshot (105).png

step 3:

select option of Export file format for certificates - Platform independent PEM-Format

Screenshot (106).png

step 4:

copy certificate path e.g,  C:\ProgramData\Qlik\Sense\Repository\Exported Certificates

step 5:

open command prompt and  type: pip install websocket-client

step 6:

Python code :

from websocket import create_connection

import ssl

senseHost = "dummy.xyz.com"

privateKeyPath = "C:\ProgramData\Qlik\Sense\Repository\Exported Certificates\"

## userDirectory and userId can be found at QMC -> Users

userDirectory, userId = "DIRECTORY_OF_SERVER","QlikServerUserId"

url = "wss://" + senseHost + ":4747/app"  # valid

certs = ({"ca_certs": privateKeyPath + "root.pem",

            "certfile": privateKeyPath + "client.pem",

            "keyfile": privateKeyPath + "client_key.pem",

            "cert_reqs":ssl.CERT_REQUIRED,

            "server_side": False

            })

ssl.match_hostname = lambda cert, hostname: True

ws = create_connection(url, sslopt=certs,

                       header={'X-Qlik-User: UserDirectory=%s; UserId=%s'% (userDirectory, userId)})

session = self.ws.recv()

### after this you can get help of qlik-engine-api to perform operations. 😉

          "

ion = self.ws.recv()

Thank you.

Comments
arulsettu
Master III
Master III

Hi

Were you able to connect and open the qlik apps successfully ?

0 Likes
bhargavpatel431
Contributor II
Contributor II

Yes, i was able to connect qlik sense.

0 Likes
palashpandey9
Contributor
Contributor

Hey! Great example. I just have one question, how do I send json to the engine API using this example? I am trying to get data from my apps using the engine API.

Thank you in advance!

0 Likes
harika18
Contributor III
Contributor III

Should we write this code on client system or server machine?

 

0 Likes
mohitp
Contributor
Contributor

Hello,

Can you please help in making changes to this code to implement it on the client side?

I am getting the below error:

Traceback (most recent call last):
File "Code 02.py", line 52, in <module>
header={'X-Qlik-User: UserDirectory=%s; UserId=%s'% (userDirectory, userId)})
File "C:\Program Files\Python37\lib\site-packages\websocket\_core.py", line 514, in create_connection
websock.connect(url, **options)
File "C:\Program Files\Python37\lib\site-packages\websocket\_core.py", line 223, in connect
options.pop('socket', None))
File "C:\Program Files\Python37\lib\site-packages\websocket\_http.py", line 126, in connect
sock = _ssl_socket(sock, options.sslopt, hostname)
File "C:\Program Files\Python37\lib\site-packages\websocket\_http.py", line 260, in _ssl_socket
sock = _wrap_sni_socket(sock, sslopt, hostname, check_hostname)
File "C:\Program Files\Python37\lib\site-packages\websocket\_http.py", line 239, in _wrap_sni_socket
server_hostname=hostname,
File "C:\Program Files\Python37\lib\ssl.py", line 423, in wrap_socket
session=session
File "C:\Program Files\Python37\lib\ssl.py", line 870, in _create
self.do_handshake()
File "C:\Program Files\Python37\lib\ssl.py", line 1139, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: invalid CA certificate (_ssl.c:1076)

0 Likes
kevinpintokpa
Creator II
Creator II
I set up the sslopts as follows: 2020-01-27 21:15:03 -0700 DEBUG: {'ca_certs': '/Path/root.pem', 'certfile': '/Path/client.pem', 'keyfile': '/Path/client_key.pem', 'server_side': False} I am getting this error message: 2020-01-27 21:15:03 -0700 ERROR: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056) Any ideas?
JeromeS
Partner
Partner

Hi, 

I got both errors. don't forget to add root.pem in the trusted certificates in the MMC 

https://support.qlik.com/articles/000026086

You can also choose to disable ssl certificate verification by setting ssl.CERT_NONE but i believe this is not recommended

certs = ({"ca_certs": privateKeyPath + "root.pem",
"certfile": privateKeyPath + "client.pem",
"keyfile": privateKeyPath + "client_key.pem",
"cert_reqs": ssl.CERT_NONE,
"server_side": False
})

 

 

0 Likes
Version history
Revision #:
1 of 1
Last update:
‎2018-03-14 02:45 AM
Updated by: