krishnavelu / alice_blue

Official Python library for Alice Blue API trading
GNU General Public License v3.0
134 stars 82 forks source link

KeyError: 'userSession' while generating sessionID #484

Closed nrameshy closed 1 year ago

nrameshy commented 1 year ago

For the last 3 days I have been observing the below error while generating session id using alice blue api(Version: 2.0.3).

Traceback (most recent call last): File "/home/nrameshy/mkt_data/scripts/get_ab_sessions.py", line 4, in <module> get_session_ids(access_file) File "/home/nrameshy/mkt_data/scripts/ab3_methods.py", line 33, in get_session_ids raise e File "/home/nrameshy/mkt_data/scripts/ab3_methods.py", line 19, in get_session_ids session_id = AliceBlue.login_and_get_sessionID(username = r.username, File "/home/nrameshy/.local/lib/python3.8/site-packages/alice_blue/alice_blue.py", line 232, in login_and_get_sessionID session_id = r.json()['userSession'] KeyError: 'userSession' I tried to print the r.json() and prints the below info {'emsg': 'Enable/Login with TOTP, to create the session.', 'stat': 'Ok'}

If I login to web/mobile app using TOTP and rerun my script to generate sessionID it works fine. So is it required to login web/mobile app before generating session id using alice blue api every day? Please help

krishnavelu commented 1 year ago

No

nrameshy commented 1 year ago

@krishnavelu Could you help me to know why I am getting this error?

aeron7 commented 1 year ago

Alice blue team has officially said this in the telegram group -

Users should log in through the Web(ant.aliceblueonline.com) or SSO or Mobile at least once a day, before connecting the API.

But they have changed and still changing many aspects of API rapidly, so unsure if this will persist or go away in the future.

nrameshy commented 1 year ago

Thank you @aeron7 for the info

PythonNovice5 commented 1 year ago

Hi, I also started facing this issue from November 4, 2022 onwards. If I login manually first in the application, then only my code works. Please don't close this issue until we find some resolution @krishnavelu Please help.

krishnavelu commented 1 year ago

you have to login through app or web before doing api login. No workaround for this.

srinidhihebbar260297 commented 1 year ago

You can use selenium to login pre the code and also pull the API secret and connect it to the official wrapper, we can set up authenticator TOTP and login

krishnavelu commented 1 year ago

@srinidhihebbar260297 Stop promoting the stolen code here. Do somewhere else.

srinidhihebbar260297 commented 1 year ago

Just promoting the code with lesser errors while execution @krishnavelu

aeron7 commented 1 year ago

Just promoting the code with lesser errors while execution @krishnavelu

Lower execution? The only reason Alice's CTO is Alice's CTO is because of his bootlicking skills.

You can use selenium to login pre the code and also pull the API secret and connect it to the official wrapper, we can set up authenticator TOTP and login

Auto Login is barred as per SEBI rules. Otherwise, The method would've been added here.

Anyways, Anyone who uses or suggests someone to use Selenium to get API Secret must leave Algo Trading for his own good's sake! Ever heard of Python Requests Module?

aeron7 commented 1 year ago

Refer https://github.com/krishnavelu/alice_blue/issues/500#issuecomment-1386357865