I tried the below code.
import websocket
import json
api_key='PKWS2NU9W6AI09PIEL3J'
secret_key='8IgZywxHrHuViIOT5/SKvZvy3159thn6SkzSKZcV'
ticker = 'MSFT'
ticker = 'Q.'+ticker
socket = "wss://alpaca.socket.polygon.io/stocks"
def on_open(ws):
print('Market is Open')
auth = {"action":"auth", "params": api_key}
channel_data = {
"action": "subscribe",
"params": ticker
}
ws.send(json.dumps(auth))
ws.send(json.dumps(channel_data))
def on_message(ws, message):
print(message)
def on_close(ws):
print("closed connection")
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
ws.run_forever()
But the API was not authenticating! Please help me!