Can anyone please help me to find out why i am getting following error while i was verifying my connection thourgh getting my profile information? or how to get variable from this code to use further data fetching for example fyers.quotes?
{'s': 'error', 'code': -17, 'message': 'Could not authenticate the user'}
and below is my python code, Please someone help on this!
class fyers_broker(QThread):
status_update = pyqtSignal(str, str) # Signal to send messages to UI
connection_status = pyqtSignal(bool) # Broker connection status signal
def __init__(self):
super().__init__()
self.connection_attempts = 0 # Track number of connection attempts
self.connection = False
self.fyers = None
# API keys and endpoints
self.CLIENT_ID = cr.CLIENT_ID
self.APP_ID = cr.APP_ID
self.SECRET_KEY = cr.SECRET_KEY
self.FY_ID = cr.FY_ID
self.TOTP_KEY = cr.TOTP_KEY
self.PIN = cr.PIN
self.URL_SEND_LOGIN_OTP = "https://api-t2.fyers.in/vagator/v2/send_login_otp"
self.URL_VERIFY_TOTP = "https://api-t2.fyers.in/vagator/v2/verify_otp"
self.URL_VERIFY_PIN = "https://api-t2.fyers.in/vagator/v2/verify_pin"
self.URL_TOKEN = "https://api-t1.fyers.in/api/v3/token"
self.run()
# OTP Sending Logic
def send_login_otp(self):
try:
fyers = requests.post(
url=self.URL_SEND_LOGIN_OTP,
json={"fy_id": self.FY_ID, "app_id": cr.APP_ID_TYPE}
)
fyers.raise_for_status()
return fyers.json().get("request_key"), None
except Exception as e:
return None, str(e)
# Handle OTP and TOTP Generation
def generate_totp(self):
try:
return pyotp.TOTP(self.TOTP_KEY).now(), None
except Exception as e:
return None, str(e)
# Verify TOTP
def verify_totp(self, request_key, totp):
try:
fyers = requests.post(
url=self.URL_VERIFY_TOTP,
json={"request_key": request_key, "otp": totp}
)
fyers.raise_for_status()
return fyers.json().get("request_key"), None
except Exception as e:
return None, str(e)
# Verify PIN Logic
def verify_pin(self, request_key):
try:
fyers = requests.post(
url=self.URL_VERIFY_PIN,
json={"request_key": request_key, "identity_type": "pin", "identifier": self.PIN}
)
fyers.raise_for_status()
response = fyers.json()
print(f"Response from verify_pin: {response}")
return fyers.json()["data"]["access_token"], None
except Exception as e:
return None, str(e)
def run(self):
"""Main connection logic with retry mechanism."""
try:
if not self.connection:
self.connection_attempts = 0 # Reset retry attempts
# Attempt Sending OTP
self.status_update.emit("Sending OTP...",'black')
request_key, error = self.send_login_otp()
if error:
self.status_update.emit(f"OTP Failed: {error}",'red')
return
self.status_update.emit("OTP Sent. Generating TOTP...",'black')
totp, error = self.generate_totp()
if error:
self.status_update.emit(f"TOTP generation failed: {error}",'red')
return
self.status_update.emit("Verifying TOTP...",'black')
retries = 0
while retries < 3:
new_request_key, error = self.verify_totp(request_key, totp)
if error:
self.status_update.emit(f"TOTP verification failed. Retrying...",'red')
time.sleep(1) # Wait before retrying
retries += 1
else:
self.status_update.emit("TOTP Verified!",'green')
request_key = new_request_key
break
else:
self.status_update.emit("Could not verify TOTP.",'red')
return
self.status_update.emit("Verifying PIN...",'black')
access_token, error = self.verify_pin(request_key)
if error:
self.status_update.emit(f"PIN Verification failed: {error}",'red')
return
self.status_update.emit("Access token generated successfully.",'green')
print(f"chencking12:::{access_token}")
# Create the fyers object with the access token
self.fyers = fyersModel.FyersModel(client_id=self.CLIENT_ID,
token=access_token,
log_path=r"data")
print(f"Error in checking:....{self.fyers.get_profile()}")
self.connection = True
self.connection_status.emit(True) # Send a success signal
else:
self.status_update.emit("Already connected to broker.",'greed')
except Exception as e:
self.status_update.emit(f"Failed to connect due to error: {e}",'red')
self.connection_status.emit(False)
def get_fyers(self):
if self.connection:
return self.fyers
return None
fyers_broker()