I am trying to use a .pem certificate to connect to a server using websockets as required.
They required using a token and the certificate.
First, I tried to add the certificate in the header along with the token.
I got an error 413.
Then I tried the code below.
I got an unauthorized error.
What could be the solution according to you?
I am using python 3.11 and websocket-client library 1.8.0 .
<code>
import websocket
import json
import base64
def get_certificate(pem_file_path):
with open(pem_file_path, 'rb') as file:
return file.read().decode("utf-8")
def generate_auth_header(token):
header_info = {
"Authorization": f"Bearer {token}",
"host": HOST,
#"ClientCertificate": get_certificate("certificate_path.pem")
}
json_header = json.dumps(header_info)
b64_header = base64.b64encode(json_header.encode()).decode()
return b64_header
def on_open(ws, error):
print(error)
def on_message(ws, message):
print(message)
def on_close(ws, status_code, close_msg):
print(close_msg)
ws_endpoint_with_header = (
f"{url_wss}?header={generate_auth_header(TOKEN)}&payload=e30="
)
ws = WebSocketApp(
WsEndpoint,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
)
ws.run_forever(sslopt={"ca_certs": "certitiface_path.pem"})
</code>
<code>
import websocket
import json
import base64
def get_certificate(pem_file_path):
with open(pem_file_path, 'rb') as file:
return file.read().decode("utf-8")
def generate_auth_header(token):
header_info = {
"Authorization": f"Bearer {token}",
"host": HOST,
#"ClientCertificate": get_certificate("certificate_path.pem")
}
json_header = json.dumps(header_info)
b64_header = base64.b64encode(json_header.encode()).decode()
return b64_header
def on_open(ws, error):
print(error)
def on_message(ws, message):
print(message)
def on_close(ws, status_code, close_msg):
print(close_msg)
ws_endpoint_with_header = (
f"{url_wss}?header={generate_auth_header(TOKEN)}&payload=e30="
)
ws = WebSocketApp(
WsEndpoint,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
)
ws.run_forever(sslopt={"ca_certs": "certitiface_path.pem"})
</code>
import websocket
import json
import base64
def get_certificate(pem_file_path):
with open(pem_file_path, 'rb') as file:
return file.read().decode("utf-8")
def generate_auth_header(token):
header_info = {
"Authorization": f"Bearer {token}",
"host": HOST,
#"ClientCertificate": get_certificate("certificate_path.pem")
}
json_header = json.dumps(header_info)
b64_header = base64.b64encode(json_header.encode()).decode()
return b64_header
def on_open(ws, error):
print(error)
def on_message(ws, message):
print(message)
def on_close(ws, status_code, close_msg):
print(close_msg)
ws_endpoint_with_header = (
f"{url_wss}?header={generate_auth_header(TOKEN)}&payload=e30="
)
ws = WebSocketApp(
WsEndpoint,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
)
ws.run_forever(sslopt={"ca_certs": "certitiface_path.pem"})