Automated Fyers API login asking for human verification. What is the solution?

Today, automated Fyers API login is asking for human verification. Because of this, not able to use API. Any solution to this?

yeah, observing same issue. Why would user need to do human verification for automated login?

I’m trying to use selenium to beat automate the captcha but it’s not working. If someone succeeds please tell me.

RIght now, I am doing it manually. You use the algo to login for you but you can copy paste the fyers link and do the login yourself using OTP (as TOTP changes so quickly even my PC misses it sometimes.) and the link you get at the end has some stuff in between authcode= and &None. That’s your auth code.

In the below code, if you just change the url that get after logging in, it will save your auth code in a file from where you can access it.

Import the required module from the fyers_apiv3 package

from fyers_apiv3 import fyersModel
import credentials as cd

client_id = cd.client_id
secret_key = cd.secret_key
redirect_uri = cd.redirect_uri
user_name = cd.user_name
totp_key = cd.totp_key
pin1 = cd.pin1
pin2 = cd.pin2
pin3 = cd.pin3
pin4 = cd.pin4

Replace these values with your actual API credentials

response_type = “code”
state = “sample_state”

Create a session model with the provided credentials

session = fyersModel.SessionModel(
client_id=client_id,
secret_key=secret_key,
redirect_uri=redirect_uri,
response_type=response_type
)

Generate the auth code using the session model

response = session.generate_authcode()

newurl = “Google
auth_code = newurl[newurl.index(‘auth_code=’)+10:newurl.index(‘&state’)]
print(auth_code)

Import the required module from the fyers_apiv3 package

from fyers_apiv3 import fyersModel

Define your Fyers API credentials

response_type = “code”
grant_type = “authorization_code”

Create a session object to handle the Fyers API authentication and token generation

session = fyersModel.SessionModel(
client_id=client_id,
secret_key=secret_key,
redirect_uri=redirect_uri,
response_type=response_type,
grant_type=grant_type
)

Set the authorization code in the session object

session.set_token(auth_code)

Generate the access token using the authorization code

response = session.generate_token()

Print the response, which should contain the access token and other details

print(response)

access_token = response[‘access_token’]

with open(‘access.txt’,‘w’) as k:
k.write(access_token)


my semi automatic code for generating accessing token .

केवल OTP और PIN को डालना है।

और अंत में Web Browser के URL को copy करना है।]

######### CODE START ##################################

import time

import datetime

import pyotp

import pickle

import requests

import pyperclip

import webbrowser

from pprint import pprint

from fyers_apiv3 import fyersModel

from urllib.parse import parse_qs,urlparse

def generate_access_token() :

global user_id, pin, app_id, api_key, redirect_url, totp_key, url

print(“\nGenerating Access Token …”)

try :

user_id = "YOUR USER ID "

pin = "YOUR PIN

app_id = "YOUR APP ID "

api_key = “YOUR API KEY”

redirect_url = “YOUR REDIRECT ID”

totp_key = “YOUR TOTP KEY”

session = fyersModel.SessionModel(

client_id = app_id,

secret_key = api_key,

redirect_uri = redirect_url,

response_type = “code”,

state=" ")

url = session.generate_authcode()

webbrowser.open_new(url)

old_clipboard_contents = pyperclip.paste()

new_clipboard_contents = pyperclip.paste()

while old_clipboard_contents == new_clipboard_contents :

new_clipboard_contents = pyperclip.paste()

time.sleep(1)

url = new_clipboard_contents

parsed = urlparse(url)

auth_code = parse_qs(parsed.query)[“auth_code”][0]

session = fyersModel.SessionModel(

client_id = app_id,

secret_key = api_key,

redirect_uri = redirect_url,

response_type = “code”,

grant_type = “authorization_code”)

session.set_token(auth_code)

response = session.generate_token()

access_token = response[“access_token”]

refresh_token = response[“refresh_token”]

token_dict = {“app_id”: app_id,

“access_token”: access_token,

“refresh_token”: refresh_token}

with open (“token_dict.pickle”, “wb”) as file :

pickle.dump(token_dict, file)

except :

time.sleep(2)

generate_access_token()

def fyers_login() :

global fyers, token_dict, name

while True :

try :

with open(“token_dict.pickle”, “rb”) as file :

token_dict = pickle.load(file)

#print(token_dict)

except Exception as error :

print(error)

token_dict = {“app_id”: 0, “access_token”: 0, “refresh_token”: 0}

fyers = fyersModel.FyersModel(client_id=token_dict[“app_id”], is_async=False, token=token_dict[“access_token”], log_path=log_data_path)

response = fyers.get_profile()

if response[“s”] == “error” :

generate_access_token()

else :

print(“\nlogin Details …”)

pprint(response[“data”])

name = response[“data”][“name”]

#print(name)

print(“”)

print()

break

fyers_login()

########## CODE END #########################

Thanks! Manual login was not a problem. I was trying to eliminate the human verification in auto-login.

Thanks! Doing similar login for the time being.

Same here! Lets share if anyone gets the solution.

Hi Everyone,

Due to security concerns and compliance regulations, FYERS does not support auto-login.

Despite this, some users have managed to implement auto-login. To address this, we have enforced additional security measures.

Please follow the recommended login flow detailed in our API documentation: https://myapi.fyers.in/docsv3#tag/Authentication-and-Login-Flow-User-Apps

We appreciate your cooperation.

The code mentioned on the API documentation page is below. It doesn’t give anything, (have tried it after changing the credentials) -

from fyers_apiv3 import fyersModel

Replace these values with your actual API credentials

client_id = “SPXXXXE7-100”
secret_key = “N********B”
redirect_uri = “API - Sample Redirect URI
response_type = “code”
state = “sample_state”

Create a session model with the provided credentials

session = fyersModel.SessionModel(
client_id=client_id,
secret_key=secret_key,
redirect_uri=redirect_uri,
response_type=response_type
)

Generate the auth code using the session model

response = session.generate_authcode()

Print the auth code received in the response

print(response)

If manual login is required everyday, it is a deal-breaker for any algo-trader.

I have similar issue. I put in the OTP manually after the url opens. Now when I put in the mobile number for OTP and check the box for human verification, it fails. Don’t know what to do. I am not very good at programming. I just use common sense and logic to code (mostly copied from internet and then applied my own logic, worked perfectly well untill fyers started asking captcha). If someone can help.

Hi @hiren_banker
No worries, please DM your client ID, we will assist you

Hi Shreyash Patel

Thanks for sharing login code indeed a great help. Can you post again with clear indention. I am not able to understand where while loop ends when I was trying to integrate your code.

Thank you in advance

access_token.pdf (49 KB)

आप PDF देखे। अगर फिर भी \समस्या हो तो आप ईमेल ID दे।

Thank you.

Sushovan

[email protected]

If we have to do this type of solution then which is meant to stop automation then whats the use of api .

not using it any more

Correct Animesh, api with manual auth what a joke

Hi @108683832891228327815 ,

Auto login via OTP is unsecured and not recommended as per the compliance and regulatory security requirements.

Having said that, @naresh_janagam can help you achieve the same via TOTP method.

Please let him know if you would like to explore.

Thanks,

Pranav

bro have got the solution for automation

Hi

Automated Fyers API login asking for human verification.
Not able to pass human verification stage via Python code.
Do we have any solution for that ?
OR
Is there any way to skip that part?

Earlier when human verification was not there, I was able to use TOTP method.
but its hard to simulate human now :slightly_smiling_face:

Any help would be appreciated.

Thanks