Hello can anyone help me in setting up API connection with fyers, everytime i doing it is giving me below error. I have the api subscription from fyers but don't know why it is giving me the error. below is the code please can you advise what else do i need to input.
import requests
import websocket
import json
# Constants
CLIENT_ID = "your_client_id" # Replace with your Fyers client ID
ACCESS_TOKEN = "your_access_token" # Replace with your Fyers access token
FYERS_REALTIME_URL = "wss://api.fyers.in/socket/v2/datafeed"
def on_message(ws, message):
"""Callback to handle incoming WebSocket messages."""
print("Received Data: ", message)
def on_error(ws, error):
"""Callback to handle WebSocket errors."""
print("WebSocket Error: ", error)
def on_close(ws, close_status_code, close_msg):
"""Callback when WebSocket closes."""
print("WebSocket Closed")
def on_open(ws):
"""Callback when WebSocket connection is opened."""
print("WebSocket Connection Established")
# Subscribe to market data for a specific symbol
subscription_request = {
"symbol": "NSE:RELIANCE-EQ", # Replace with your desired symbol
"type": "subscribe"
}
ws.send(json.dumps(subscription_request))
print("Subscription Request Sent: ", subscription_request)
def fetch_realtime_data():
"""Main function to establish WebSocket connection and fetch data."""
headers = {
"Authorization": f"Bearer {ACCESS_TOKEN}"
}
ws = websocket.WebSocketApp(
FYERS_REALTIME_URL,
header=headers,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
ws.on_open = on_open
ws.run_forever()
if name == "__main__":
fetch_realtime_data()
Error that i am getting is Token is not valid even nthough i entering correct token.