Guide to secure your SmartAPI Account with two factor authentication
-
@ravitandur It is same API, so should be working. only extra totp need to be sent.
-
@Ashok what i meant is you can't get this secret by just username and password as Anil is asking. One has to go to enable otp page to get this code one time.
-
@aj_Brk correct
-
@babitakundu i guess you have to manually download the repo and run python setup.py install. I dont see the code yet updated on pip
-
@aj_Brk Admins are adding in pip. If you want to try meanwhile, edit directly in local or download from github.
-
@babitakundu just replace the generatesession def with new one. it worked for me. You have to manually edit in source file
-
@aj_Brk code generated from "pyotp" not working for me, getting below output with your code. Any suggestion. If I am using the code from DUO app I am able to login successfully
data = obj.generateSession(clientCode, password, totp.now())
print(data)Shell output:
pyotp: 586380
{'status': False, 'message': 'Invalid totp', 'errorcode': 'AB1050', 'data': None} -
@milind017 this need to do like this.
totp = pyotp.TOTP(s='secret key in qr uri after qr generation'
And then the generate session.
-
@Ashok Yes did the same. Still getting the same
obj = SmartConnect(api_key=historyApiKey)totp = pyotp.TOTP(s='secret key in qr uri after qr generation')
print("pyotp",totp.now())attempts = 5
while attempts > 0:
attempts = attempts-1
data = obj.generateSession(clientCode, password, totp.now())
print(data)
if data['status']:
break
time.sleep(2) -
-
@SSumit
I have tried with shared link code, still getting the same
Shell Output:
with TOTP: 932310
With parse_uri: 932310
{'status': False, 'message': 'Invalid totp', 'errorcode': 'AB1050', 'data': None} -
@milind017 I see that you have put condition in if data['status'] , is it going in retry at all ?
-
@milind017 try with this condition
if not data['message'].contains('Invalid totp'):
break -
@milind017 @aj_Brk why retry, it passed almost always on first time. Anyway. Not sure why parse_uri is needed.. also just pass totp as a string
-
TypeError: SmartConnect.generateSession() takes 3 positional arguments but 4 were given
-
@Ashok Actually you are right it works most of the time , but for those rare instances where by the time your request went and it expired on server side , i had added retry. It happened to me once perhaps due to slow code execution or internet.
-
@gokul try the latest library - pip install smartapi-python --upgrade . It appears the new version is live now
-
@mudit236 @babitakundu It appears it is updated now. Please try - pip install smartapi-python --upgrade
-
@Ashok Yes I have already tried with totp as string still its same
Yes retry not needed by still to be sure.
I have added both code to check if I am getting the same totp, which I am getting but still not able to get the success
Is pyotp is working for you? -
@milind017 @Ashok @gokul @Meenavenkit
Totp changes have been deployed in the latest python release 1.3.0
Please use the following command to upgrade to the latest python release
pip install smartapi-python --upgrade