Invalid Token for Historical API

xaqpex
Hello,

Trying to get the historical data for an instrument throws an Invalid Token Exception.

data_from = date.today() + timedelta(days=-100)
data_to = date.today() + timedelta(days=-1)

data = kite.historical_data(data_from, data_to, 3535361, "day", True, False)


The error returned is kiteconnect.exceptions.InputException: invalid token

I subscribed for the historical data AFTER subscribing to the API

I had already created request & access tokens for the day before subscribing to the Historical API

I have tried logging out and logging back in but this does not change it.

I suspect that the access tokens for the day which are saved in Zerodha's database might have some permissions associated to it at the time of saving them.
So logging out and logging back is not useful as the access token is not changing.

I am losing a day of subscription as I cannot proceed.

Please advise.

Regards,
Warren
  • sujith
    Can you run pykiteconnect in debug mode and paste the complete stack trace here?
    Make sure to remove app and client specific tokens.
  • xaqpex
    As I am new to kiteconnect, can you please explain what you mean by
    run pykiteconnect in debug mode

    Can you share the script line or an example or a link to someone else having the issue which has some code sample.

    I commented out
    data = kite.historical_data(data_from, data_to, 3535361, "day", True, False)

    and added
    stock = kite.quote('NSE:POWERGRID')

    It is working fine with this one line replacement.

    Going back to the historical line throws the exception
    kiteconnect.exceptions.InputException: invalid token
  • sujith
    You can initialize kiteconnect with debug flag true. You can check out the documentation here.
  • xaqpex
    I have updated it to
    kite = KiteConnect(api_key, access_token, None, True)

    I am still getting the below error
    Traceback (most recent call last):
    File "/var/www/html/Python/zerodha/src/import_history.py", line 162, in
    import_history()
    File "/var/www/html/Python/zerodha/src/import_history.py", line 84, in import_history
    kite.historical_data(data_from, data_to, 3535361, "day", True, False)
    File "/var/www/html/Python/zerodha/venv/lib/python3.9/site-packages/kiteconnect/connect.py", line 621, in historical_data
    data = self._get("market.historical",
    File "/var/www/html/Python/zerodha/venv/lib/python3.9/site-packages/kiteconnect/connect.py", line 836, in _get
    return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
    File "/var/www/html/Python/zerodha/venv/lib/python3.9/site-packages/kiteconnect/connect.py", line 912, in _request
    raise exp(data["message"], code=r.status_code)
    kiteconnect.exceptions.InputException: invalid token
  • xaqpex
    Is this what was needed? Please advise as I am not able to move forward with this.
  • sujith
    It looks like you are not send a valid instrument token.
    You can check out the pykiteconnect documentation, it looks like your order of params is wrong.
  • xaqpex
    Please amend the exception message, there is request token, access token and instrument token.
    It would be helpful to have known this earlier.
Sign In or Register to comment.