Is it necessary to register a redirect url even to simply consume streaming market data in 3.0?

sidverm
I have been consuming streaming market data from KiteConnect v2 for over 6 months now. I used Python client for Websocket API. Today I tried to migrate my code to latest KiteConnect 3.0, and realized the interface for Websocket class is changed from:
kws = WebSocket("your_api_key", "your_public_token", "logged_in_user_id")
to:
kws = KiteTicker("your_api_key", "your_access_token")
on github, API doc https://github.com/zerodhatech/pykiteconnect/tree/kite3, I found the following comment:

# Redirect the user to the login url obtained
# from kite.login_url(), and receive the request_token
# from the registered redirect url after the login flow.

And similar comment from https://kite.trade/docs/connect/v3/

"An api_key + api_secret pair is issued and you have to register a redirect url where a user is sent after the login flow. For mobile and desktop applications, there has to be a remote backend which does the handshake on behalf of the mobile app and the api_secret should never be embedded in the app"

I don't have any url, and probably might not have any in forseeable future. My intention is to simply use this API to capture streaming data and consume it without sending any order. Ill use my own zerodha account for consuming this data.

I have been able to do so easily in v2. Am I not going to be able to do so in v3?

Regards,
Siddharth

  • sujith
    Hi @sidverm,
    The new version of Kite Ticker expects you to provide a valid access token in order to establish a connection.

    You have already provided a redirect URL for the Kite Connect app, it is http://127.0.0.1
  • sidverm
    sidverm edited March 2018
    Hi @sujith,
    Ah, I didn't realize that dummy url would work in this case. After your assurance, I just tried using it, but got error.

    # Initialise Kite
    kite = KiteConnect(api_key="my_api_key")

    request_token = kite.login_url('http://127.0.0.1')

    data = kite.generate_session(request_token, api_secret="my_api_secret")

    kws = KiteTicker("my_api_key", data["access_token"])

    This gave an error: kite.login_url() needs exactly 1 input, 2 provided.

    So, I tried the variant of above code with simply

    request_token = kite.login_url()

    But that gave another error: invalid token.

    I am confused now. How do I get request_token?

    Regards,
    Siddharth
  • sujith
    Please go through the documentation and make sure you watch the webinar.
  • sidverm
    sidverm edited March 2018
    @sujith thanks for pointing me to the doc. After reading and trying stuff, I have inched forward, but still facing some issue. Here is what I have done so far.

    When I manually wrote the below url on my browser.
    https://kite.trade/connect/login?v=3&api_key=xxx
    I got redirected to Zerodha login screen. There I entered by login details. And I got redirected to a page that looked like below.


    I clicked Authorize, and then I got redirected to something that looked so pleasing to my eyes!
    http://127.0.0.1/?status=success&request_token=ET319oeAUJodJ5mRte7yADoXdYFz0OZ3

    The stuff next to request_token= is what I needed. I was a bit worried how logging in and clicking "authorize" would work programatically, but to my relief, this was only a one time drill. The second time I manually wrote the below on my browser
    https://kite.trade/connect/login?v=3&api_key=xxx

    I was immediately redirected to:
    http://127.0.0.1/?status=success&request_token=ET319oeAUJodJ5mRte7yADoXdYFz0OZ3

    Alas, the joy ended prematurely.

    When I tried this programatically in Python, this redirection didn't happen. Here is my python code. Can you help me spot what's wrong here?
    from kiteconnect import KiteTicker
    from kiteconnect import KiteConnect

    # Initialise Kite
    kite = KiteConnect(api_key="my_api_key")

    response = requests.get(kite.login_url())
    print(response.url)
    for r in response.history:
    print(r.url)

    Output:
    https://kite.zerodha.com/connect/login?sess_id=ty6j78gfskoslk6zxe69gp1hsntq051j&api_key=my_api_key&v=3
    https://kite.trade/connect/login?api_key=my_api_key&v=3
    https://kite.zerodha.com/connect/login?api_key=my_api_key&v=3
    I've spent hours trying to find "request_token" anywhere in response object or my registered url, "http://127.0.0.1" but in vain. Please help me!
  • zartimus
    @sidverm Check this out https://github.com/zerodhatech/pykiteconnect/blob/kite3/examples/flask_app.py
    This gives you an access token. Use this while initialising kite ticker.
  • sidverm
    @zartimus, thanks for pointing this code out. But I am not sure how to use it.
    I have tried
    1) download flask_app.py
    2) modify below lines with real values
    kite_api_key = "kite_api_key"
    kite_api_secret = "kite_api_secret"
    3) run on commandline
    > python flask_app.py
    But that prints "starting webserver at 127.0.0.1" etc etc. Doesn't print any access token. And doesn't even get killed by Ctrl-C. Can you please tell me the exact steps to get access token programatically via python?
  • zartimus
    zartimus edited March 2018
    @sidverm
    Assuming you are using kite3,

    Change your redirect uri to http://127.0.0.1:5010/login on kite developer console (to match this https://github.com/zerodhatech/pykiteconnect/blob/kite3/examples/flask_app.py#L40).

    run python flask_app.py, Open 127.0.0.1 on your browser, press login to generate access token. This redirects you to login page. If the oauth succeeds, your request token will be send back to /login endpoint on your local flask server. https://github.com/zerodhatech/pykiteconnect/blob/kite3/examples/flask_app.py#L99 generates the access token from the request token. Bingo! your access token will be displayed on the page itself now.
  • sidverm
    Thanks. But I want to make one more thing clear to you. I have a daily cron set-up which currently runs kite2 (I mean not kite3). I am in process of migrating it to kite3. That cron job basically starts in the morning, and starts listening to full quote data of a set of instruments, and pushes that data into a database for my later use.

    In kite3, if I have to do the same, I can't be using any manual steps like "Open 127.0.0.1 on your browser, press login to generate access token". I need my code to do this in my absence every morning. Unless you tell me that "access token" once obtained by the manual process is usable every-day. If so, I will simply hard-code it in my code. But if needs to be generated in every session, then I cannot have any manual steps. Please suggest a completely programmatic way.
  • sujith
    @sidverm,
    It is mandatory by the exchange that a user has to log in manually at least once a day. We don't recommend automating log in.
  • sidverm
    @sujith, really? I am not trading, just listening to market data. How can I possibly harm anyone by that? Is this documented somewhere on exchange website or just informed to all brokers?
  • sujith
    Hi,
    A member/broker can only give data to their clients. The only way to validate it is by logging in.
    You may also directly subscribe for live market data from the exchange directly, you can check out pricing here.
This discussion has been closed.