Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receiving 400 Error #1

Closed
anilwarbhe opened this issue May 17, 2024 · 15 comments
Closed

Receiving 400 Error #1

anilwarbhe opened this issue May 17, 2024 · 15 comments

Comments

@anilwarbhe
Copy link

I tried running you code 👍
from kitetrader import Kite
kite = Kite()
kite.profile()
kite.holdings()

But Receiving error:
File "venv/lib/python3.11/site-packages/kitetrader/Kite.py", line 257, in _req
raise ConnectionError(f"{hint} | {code}: {reason}")
ConnectionError: Profile | 400: Missing or bad request parameters or values

@anilwarbhe
Copy link
Author

NOw Receiving :
File "/venv/lib/python3.11/site-packages/kitetrader/Kite.py", line 253, in _req
raise ConnectionError(f"{hint} | {code}: {reason}")
ConnectionError: TwoFA | 403: Session expired or invalid. Must relogin

@BennyThadikaran
Copy link
Owner

Hi, I see the problem at my end. Let me have a look and get back to you. Thanks for raising the issue.

@BennyThadikaran
Copy link
Owner

I used Kite last evening and everything was working well. I suppose Zerodha has introduced changes or this is a temporary issue.

I'll have a look in the evening. If the issue persists, will look into it further.

@sarekv
Copy link

sarekv commented May 18, 2024

Chiming in to say the same. Also facing the issue on my end. Doesn't matter if cookies file is deleted or exists. checked passwords and passed values multiple times, always returns 400 error -

File "/home///venv/lib/python3.11/site-packages/kitetrader/Kite.py", line 197, in _req
raise ConnectionError(f"{hint} | {code}: {reason}")
ConnectionError: Profile | 400: Missing or bad request parameters or values

Also thanks for sharing this awesome project with us :). If you need any testing help, give us a shout!

@BennyThadikaran
Copy link
Owner

@sarekv Its nothing to do with the code. The change is server side. Either a missing header or cookie value is causing the error. I will run tests on Monday as this might take some time.

@BennyThadikaran
Copy link
Owner

BennyThadikaran commented May 20, 2024

I tried to get this fixed but so far failed. It looks like cloudflare is rejecting requests based on some parameter. I had a similar problem with NSE blocking requests orginating from other servers.

Making a request through curl does work. So a workaround might still be possible. But I wont commit to this immediately.

The issue is not isolated to this library. Others are also facing the same issue.

@Venkatesha-Iyengar
Copy link

Venkatesha-Iyengar commented May 21, 2024

@BennyThadikaran, I tried manually as well and the requests are failing when we use enctoken for Authorization in headers.
There is no issue with web login and fetching the enctoken, its failing for any request which needs Authorization and we send enctoken.
Attaching screenshot for reference. Hope this helps to root cause the issue.

Screenshot 2024-05-21 at 8 26 52 AM

@Pkaran01
Copy link

Any resolution?

@BennyThadikaran
Copy link
Owner

Like i said before, the requests are being proxied through Cloudflare and are being rejected.

My understanding is requests using enctoken authorization header is being rejected. Since pykiteconnect uses a combination of api key and access token for their header.

Also Kite Web uses different endpoints in the browser, though under the hood they're likely using the same API. So anyone accessing the api using KiteConnect endpoints and using enctoken authorization headers are being rejected.

It is clear, they are not pleased with unofficial libraries accessing their data. Lets respect that.

I will wait a few weeks and see how things play out. Its unlikely Zerodha will rollback these changes.

Meanwhile there is interesting discussion on Kite blog. 😄
https://kite.trade/forum/discussion/14034/getting-bad-request-on-kite-ltp-nse-infy

@Pkaran01
Copy link

Every broker provide there API is in free but zerodha takes 2k monthly also need to pay high brokrage. It's high time to switch other broker. Zerodha should remove zero from there name.

@rishu1988
Copy link

All the methods except(instruments, ltp, quote) are working fine using "https://kite.zerodha.com/oms" as base url with enctoken.
instruments method is also working fine with base url "https://api.kite.trade" as it doesn't require any token.

for the ltp and quote methods, I think we can use Zerodha's websocket using kiteticker module(works with enctoken).
We can subscribe all the required instruments and get live tick by tick data for all the subscribed instruments, latest tick data for all the instruments can be kept in Redis. ltp and quote method can be changed to get data from redis for the requested instruments.

@rishu1988
Copy link

I am trying to develop this but facing some lag in getting tick data. I have subscribed around 400 instruments, after few seconds it starts getting slow. Not sure if there is any issue at my end or something is wrong from Zerodha web sockets.

@BennyThadikaran
Copy link
Owner

@rishu1988

The Kite web endpoints should definitely work. I just dont want to put in the work on that front because it is only a matter of time that Zerodha will introduce changes to secure it.

If Kite Websockets works, you can have a look at Aio-Trader. I built it using aio-http library. It has some speed optimizations though i doubt it makes any overall difference from PyKiteConnect. The major advantage is a reduced dependencies.

I may switch to using Fyers or Dhan in the future for my scripts.

Instead of Redis you can use SQLite which is part of python standard library and easy to setup. An in-memory sqlite database should be fast enough depending on how many instruments you're working with. Instead of updating the Db on very tick, store the ticks in a Python list and update every 10 secs or 1 min depending on your requirements. At some interval like 5 mins, you can run a function to extract tick data and convert to OHLC and store it whereever you need. You can use APScheduler to run at intervals.

I've had plans since a long time to implement something like this. If you choose to open source your work, I'd be happy to contribute.

BennyThadikaran added a commit that referenced this issue Jun 1, 2024
Added not updating status on #1
@BennyThadikaran
Copy link
Owner

I am closing this issue as there is nothing to be done here. Anyone wishing to fork this library and implement a workaround is free to do so.

@BennyThadikaran
Copy link
Owner

I have just released v3.0.2 which allows downloading historical data and instruments with Kite Web login. The rest of the methods will be unavailable on Kite Web. Run pip install -U kitetrader to update to the latest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants