Access token issue

premjit
premjit edited April 2021 in Python client
Hi ,

I am facing access token issue.

I am repeatedly getting the same access token and unable to use the same.

Error message - kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.
Please advise.
  • premjit
    premjit edited April 2021
    The code was working till yesterday.
    @sujith kindly advise
  • rakeshr
    I am repeatedly getting the same access token and unable to use the same.
    Are you sure about using the correct access_token, generated from login flow?
    You can go through this thread to about same issue.
  • premjit
    @rakeshr , The code was working for the last 2 weeks.
    I went through the communication thread and verified that the api_key and api_secret file does not have any carriage return.

    Below is the login code which I am using for the last few weeks.

    from kiteconnect import KiteConnect
    # api_key is in my_api_key.txt file
    my_api_key = open('/Users/macdev1/PycharmProjects/SMLogin2/input_data/my_api_key.txt', 'r').read()
    # api_secret is in my_api_secret.txt file
    my_api_secret = open('/Users/macdev1/PycharmProjects/SMLogin2/input_data/my_api_secret.txt', 'r').read()

    kite = KiteConnect(api_key=my_api_key)

    # Note - section 1 and 2 and 3 are disjoint and should not be executed simultaneously
    # 1-start till request token generation, comment out the entire section after access token has been generated
    login_url = kite.login_url() #1.1
    print(login_url) #1.2
    # 1-end

    # Login into the URL and PIN and get request token
    # Paste the request token in my_request_token.txt
    # Use this to delete your access token
    #my_access_token = open('/Users/macdev1/PycharmProjects/SMLogin2/input_data/my_access_token.txt', 'r').read()
    # kite.invalidate_access_token(my_access_token)

    # 2-start Use this
    my_request_token = open('input_data/my_request_token.txt','r').read() #2.1
    input_data = kite.generate_session(my_request_token, my_api_secret) #2.2
    kite.set_access_token(input_data["access_token"]) #2.3
    print(input_data["access_token"]) #2.4
    my_access_token_file = open("input_data/my_access_token.txt","w") #2.5
    my_access_token_file.write(input_data["access_token"]) #2.6
    my_access_token_file.close() #2.7
    #2-end

    # 3-start - use access token that was saved earlier and comment section 1
    my_access_token = open('/Users/macdev1/PycharmProjects/SMLogin2/input_data/my_access_token.txt', 'r').read() #3.1
    kite.set_access_token(my_access_token) #3.2
    # 3-end

    Please advise. I have tried invalidating the access token and tried all over again but that too did not work.
  • premjit
    I have tried again 3 times and facing the same error. The access token that is getting generated is the same as the last one and this will continue to happen unless you invalidate the access token and after having a new access token, getting message - Token is invalid or expired.
  • rakeshr

    my_api_key = open('/Users/macdev1/PycharmProjects/SMLogin2/input_data/my_api_key.txt', 'r').read()
    my_api_secret = open('/Users/macdev1/PycharmProjects/SMLogin2/input_data/my_api_secret.txt', 'r').read()
    Can you re-check on api_key and api_secret file read part, that it's sent as string?
  • premjit
    I have verified, both are of str types
  • sujith
    Can you run in debug mode and check what is the status code and exact error message?
  • sujith
    You may also paste the complete stacktrace here by removing app and client specific keys.
  • premjit
    Hi @sujith , I am a novice programmer but my code was running ok for more than last few weeks. Please advise how I can run in debug mode. A short one on one session might close this issue faster.
  • smarttrader
    My suggestion - add the api key and secret as constants within the code and try. That way you would know for sure if the values you are sending atleast correct.
  • rakeshr
    Please advise how I can run in debug mode.
    You can enable debug logging in your code by adding below code:
    import logging
    logging.basicConfig(level=logging.DEBUG)
  • premjit
    premjit edited April 2021
    @smarttrader I placed the api_key and api_secret directly and tried as well. I am getting the access token as earlier but the problem is while fetching historical data I am getting the issue.
  • premjit
    @rakeshr I enabled the debug as you advised. Below are the results -
    /Users/macdev1/PycharmProjects/SMLogin2/venv/bin/python /Users/macdev1/PycharmProjects/SMLogin2/py1.py
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /session/token HTTP/1.1" 200 None
  • premjit
    premjit edited April 2021
    Below is the debug info when I am trying to fetch historical OHLCV for a few instruments -
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /instruments/historical/1793/5minute?from=2021-01-27+09%3A15%3A00&to=2021-01-27+15%3A29%3A59&interval=5minute&continuous=0&oi=0 HTTP/1.1" 200 None
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /instruments/historical/3329/5minute?from=2021-01-27+09%3A15%3A00&to=2021-01-27+15%3A29%3A59&interval=5minute&continuous=0&oi=0 HTTP/1.1" 200 None
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /instruments/historical/4583169/5minute?from=2021-01-27+09%3A15%3A00&to=2021-01-27+15%3A29%3A59&interval=5minute&continuous=0&oi=0 HTTP/1.1" 200 None
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    Start Time: 07:18:53 End Time: 07:19:02 Total time: 00:00:09
    @sujith please advise
  • premjit
    I am getting the below error
    Start Time: Thu Apr 8 13:15:28 2021
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /instruments/historical/1793/5minute?from=2021-01-27+09%3A15%3A00&to=2021-01-27+15%3A29%3A59&interval=5minute&continuous=0&oi=0 HTTP/1.1" 400 109
    Traceback (most recent call last):
    File "/Users/macdev1/PycharmProjects/SMLogin2/py2.py", line 70, in
    m1_records = kite.historical_data(token, from_date=from_date, to_date=to_date, interval=m5_interval)
    File "/Users/macdev1/PycharmProjects/SMLogin2/venv/lib/python3.6/site-packages/kiteconnect/connect.py", line 627, in historical_data
    "oi": 1 if oi else 0
    File "/Users/macdev1/PycharmProjects/SMLogin2/venv/lib/python3.6/site-packages/kiteconnect/connect.py", line 822, in _get
    return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
    File "/Users/macdev1/PycharmProjects/SMLogin2/venv/lib/python3.6/site-packages/kiteconnect/connect.py", line 894, in _request
    raise exp(data["message"], code=r.status_code)
    kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.
  • rakeshr
    rakeshr edited April 2021
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /session/token HTTP/1.1" 200 None
    Your access_token generation looks fine.
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /instruments/historical/1793/5minute?from=2021-01-27+09%3A15%3A00&to=2021-01-27+15%3A29%3A59&interval=5minute&continuous=0&oi=0 HTTP/1.1" 200 None
    Your historical data API request seems fine too.
    "GET /instruments/historical/1793/5minute?from=2021-01-27+09%3A15%3A00&to=2021-01-27+15%3A29%3A59&interval=5minute&continuous=0&oi=0 HTTP/1.1" 400 109
    kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.
    So, are you facing this exception only for Historical Data APIs or rest other API calls as well? Can you give us more info on this.
    Are you placing this from this connect app(account)? Are you using the same code as used when requests were successful?
  • premjit
    premjit edited April 2021
    Thanks @rakeshr for your response.
    As of now I am only fetching historical data and am not using any other API.
    I am using the app created in Kite.Trade and using local web server to get the request token and then generate the access token. I am able to generate the access token but when I try to fetch the data, as that point I am getting the api key error.
    Yes I was using the same code earlier and was able to fetch historical data as well.
  • premjit
    @rakeshr , @sujith
    I have tried again today and getting the same error.
    I am trying to get data for a limited no of stocks, which is failing.
    Earlier I was able to retrieve more than 400 stocks using the same code.

    {1793: 'AARTIIND', 3329: 'ABB', 4583169: 'ABBOTINDIA', 5533185: 'ABCAPITAL', 7707649: 'ABFRL', 5633: 'ACC'}
    Start Time: Sat Apr 10 16:38:55 2021
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /instruments/historical/1793/3minute?from=2021-01-27+09%3A15%3A00&to=2021-01-27+15%3A29%3A59&interval=3minute&continuous=0&oi=0 HTTP/1.1" 400 109
    Traceback (most recent call last):
    File "/Users/macdev1/PycharmProjects/SMLogin2/py2.py", line 72, in
    m1_records = kite.historical_data(token, from_date=from_date, to_date=to_date, interval=m3_interval)
    File "/Users/macdev1/PycharmProjects/SMLogin2/venv/lib/python3.6/site-packages/kiteconnect/connect.py", line 627, in historical_data
    "oi": 1 if oi else 0
    File "/Users/macdev1/PycharmProjects/SMLogin2/venv/lib/python3.6/site-packages/kiteconnect/connect.py", line 822, in _get
    return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
    File "/Users/macdev1/PycharmProjects/SMLogin2/venv/lib/python3.6/site-packages/kiteconnect/connect.py", line 894, in _request
    raise exp(data["message"], code=r.status_code)
    kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.
  • rakeshr
    Earlier I was able to retrieve more than 400 stocks using the same code.
    We haven't done any changes from our side. You need to review your code at your end.
    using local web server to get the request token and then generate the access token. I am able to generate the access token
    As said you need to check your access_token generation part. Maybe hard code request_token and then debug once. Something like:
    1> Manually copy request_token generated post successful login from your local server URL.
    2> Paste that request_token to generate_session along with API secret and check, if it successfully authenticates.
    data = kite.generate_session("paste request_token_here", api_secret="your_secret")
    print(data)
  • premjit
    Please treat the issue as closed.
    I had some issue with the virtual environment, which I have not figured out exactly.
    The issue was resolved by reimporting the env params.

    Thanks @rakeshr , @sujith for your support.
This discussion has been closed.