Margins API return error 403

Srik
Hi,

I'm trying to receive the margin details from my account using "margins" API. It is returning error 403. Can someone help me to figure whats wrong with it? Below is my code snippet.

cash = requests.get("https://api.kite.trade/user/margins/equity?api_key=xb0h8vc6v25hgf3u&access_token=a7deda4eu7d6zuzpi7gm7qhikfgccngu")

Thanks.
Srik
  • sujith
    Hi @Srik,
    It looks like your session has been expired. You will have to log in again.
  • Srik
    Hi Sujith,
    I have called KiteConnect function to log in and got the request token from the login URL. Then trying to get the access_token using request_access_token. But the access_token is always returned as 0. Can you help me resolve this issue. Thanks in advance.
  • Srik
    Also, the app in my account shows "Last login: You have not logged in to this app yet". Can you please let me know what's that I'm doing wrong.
  • sujith
    Hi @Srik,
    Check out this webinar, please follow same steps for login and get the access_token.
  • Srik
    Hi Sujith,

    I have followed the same steps and got the token earlier and used the websockets to receive the tick. Lately I added some more steps to my algorithm to get the cash information using "margins" API. From then on I'm facing issue. The API returns me invalid credentials always. Just followed the same steps as mentioned in the webinar, still the APIs throw "invalid credentials" error. Then figured out that the access_token is not valid.

    Please answer my below queries:

    1. Once we get the access_token can we keep it for ever till we invalidate the token?
    2. Is there a API to check if kiteConnect is already connected?
    3. How can I get the access_token from the login_url() automatically instead of copy pasting into the web browser and copying the acess_token form redirect string?
  • sujith
    Hi @Srik,
    Once you get access_token, it will be valid till next trading session.
    We don't have an endpoint to check whether access_token is valid or not. Whenever you make an API call, you will get 403 or token expired when your session is expired.

    For monitoring purpose, you need to use Kite and not NEST or PI. If you log into NEST/PI, all your previous sessions will be cleared and if you use kite it will reuse existing session.
    If you log out of Kite then you are logged out everywhere.

    It is mandatory by the exchange that user has to log in manually at least once a day. Hence automating login is not recommended and you will have to copy and paste request token from browser to Kite Connect.
  • nathanellisiam
    Try using a VPN service that would definitely help you out, You can try gom vpn addon. Gom vpn review is extremely good and genuine. that 's pretty nice and can help you solve you problem very easily
This discussion has been closed.