0

So I am writing a script to get financial data and I am using a broker's API. The flow goes like this. the user has to login and then it is redirected to the redirect_url where it also contains the request_token in the redirect_url. I want to extract this token from the redirect_url but the URL from requests.get is not the same as the redirect URL.

Any suggestions would be welcome.

import requests
r = requests.get(kite.login_url())
print(r.url)

This is the current code that I am using. kite is the broker's API object. Eventually I want to create a dashboard using plotly dash so if there is any solution using that also would be great.

I am using the Zerodha Kite Connect API and I have set the redirect URL to https://127.0.0.1

When I try to get r.url I get the following response:

https://kite.zerodha.com/connect/login?api_key={myapikey}&sess_id={somesessionid}

and not

https://127.0.0.1/?request_token={request_toke}&action=login&status=success

which I get when I click on the above link

  • check [this answer](https://stackoverflow.com/a/20475712/9267296) and see if that gets you what you need. Otherwise, please add more information such as the URL for the broker, the URL you are getting and a sanitized URL you want to get. – Edo Akse Jul 19 '21 at 10:38
  • I tried what this answer recommended but I am still not able to get the final redirected URL. I'll add more info. – Samik Prakash Jul 19 '21 at 10:43

2 Answers2

0

You are misunderstanding how to use the kite.login_url().

Disclaimer: I know nothing about Kite itself, but from reading I can see the login flow looks quite like any other OAuth login flow.

I'm going to assume you've set up your Kite account as per this documentation. This means setting up the API key, API secret and the redirect URL.

You need to have your app redirect the user to the URL that kite.login_url() contains.

The user is then prompted to login and accept. This is the step that you're missing. After the user does this, it states here that the URL should change to what you need.

Edo Akse
  • 4,051
  • 2
  • 10
  • 21
  • I have promted the user to go to kite.login_url() which then does redirect to my redirect URL. So how do get this redirect URL after the user has entered the details in the login_url is what I don't know. – Samik Prakash Jul 20 '21 at 14:22
  • I expect you need to run a server (using Flask fe) to get the values you need... – Edo Akse Jul 20 '21 at 14:23
0

Please set http://localhost:4004/ as your redirect URL in the Zerodha app settings.

From next login you will be redirected to a page with URL like: http://localhost:4004/request_token=XXXXXX

Get the token from the URL and you are good to go.

Destroy666
  • 892
  • 12
  • 19