access token query

Balaganga
from kiteconnect import KiteConnect
key="xxxxxxxxxxl5"
secret="xxxxxxxxxxxxxxf8u7dc"
kite=KiteConnect(api_key=key)
req_tkn="xxxxxxxxxxxxxxxxxxxxxxx3TR4xMsKI"
gen_ssn = kite.generate_session(request_token=req_tkn,api_secret=secret)
acc_tkn=gen_ssn['acccess token']
print(acc_tkn)



error which comes as


Traceback (most recent call last):
File "C:\Program Files\Sublime Text 3\first.py", line 6, in
gen_ssn = kite.generate_session(request_token=req_tkn,api_secret=secret)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Saravanan\AppData\Local\Programs\Python\Python312\Lib\site-packages\kiteconnect\connect.py", line 263, in generate_session
resp = self._post("api.token", params={
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Saravanan\AppData\Local\Programs\Python\Python312\Lib\site-packages\kiteconnect\connect.py", line 865, in _post
return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json, query_params=query_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Saravanan\AppData\Local\Programs\Python\Python312\Lib\site-packages\kiteconnect\connect.py", line 937, in _request
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.TokenException: Token is invalid or has expired.
[Finished in 2.6s]
  • MAG
    MAG edited December 2023
    @Balaganga
    Seems like there is too much delay in between you generating your request token, saving it to the variable in your python file and running the code to generate the access token. The request token expires in something like 30 seconds - not sure of the exact time.

    You have to reduce the time between the generation of the request token and the generate_session() call

    What you can do is modify your code
    Instead of the line
    req_tkn="xxxxxxxxxxxxxxxxxxxxxxx3TR4xMsKI"

    replace it with
    req_tkn=input("Please input the request token and hit enter: ")

    Then the flow will be
    1. Run this code first, it will come to the above line and pause waiting for the request token to be input.
    2. Now generate your request token, copy it and paste it at the input prompt and hit enter.
    3. The code will proceed with the "gen_ssn = kite.generate_ ..." line and create the access token with minimal delay between request token generation and the generate_session call.

    Hope this helps.


  • sujith
    @Balaganga,
    You may also refer to this thread.
  • Balaganga
    Thank you MAG and Sujith for the response.
    i changed the way of inputting and try to reduce the time gap between the request token and access token.
    but no response in output terminal.

    program:
    from kiteconnect import KiteConnect
    key="xxxxxxxxxxxxjl5"
    secret="yyyyyyyyyyyyyyyyyyyyy7dc"
    kite=KiteConnect(api_key=key)
    req_tkn=input("please input the request token and hit enter:")
    gen_ssn = kite.generate_session(request_token=req_tkn,api_secret=secret)
    acc_tkn=gen_ssn['access token']
    print(acc_tkn)


    output:
    please input the request token and hit enter:xxxxxxxxxxxxxxxxxxxxcxm



    please help.



  • sujith
    You can enable debug log while initializing pykiteconnect and see the logs.
  • Balaganga
    import logging
    from kiteconnect import KiteConnect
    logging.basicConfig(level=logging.DEBUG)
    key="xxxxxxxxxxx5"
    secret="xxxxxxxxxxxxxxxxxxxx461f8u7dc"
    kite=KiteConnect(api_key=key)
    req_tkn=input("please input the request token and hit enter:")
    gen_ssn = kite.generate_session(request_token=req_tkn,api_secret=secret)
    acc_tkn=gen_ssn['access token']
    print(acc_tkn)

    output:
    please input the request token and hit enter:xxxxxxxxxxxxxxxxxxxxxxx2P6b

    couldn't get access token
  • MAG
    @Balaganga
    There should be some error message or exception output when you ran it. What is the exception/error?
    I think I know whats wrong but I don't want to try anything now that would invalidate my access token.
    I will try your code out early morning and let you know whats wrong.
  • Balaganga
    i am interpreting in sublime
    the output, i am getting is


    please input the request token and hit enter:xxxxxxxxxxxxxxxxxxxxxxx2P6b




    after entering request token+ "enter", cursor goes to next line, that's it.(No error message)
  • MAG
    @Balaganga
    Change the line
    gen_ssn = kite.generate_session(request_token=req_tkn,api_secret=secret)
    to
    gen_ssn = kite.generate_session(req_tkn,api_secret=secret)

    This will solve the problem partially. But you still wont get the access token.
    In order to figure out the next error, add the line print( gen_ssn ) above the line where you are printing the access token. And you will automatically figure out the next error.
    Hint when I run this in the command line as python atest.py I get the following output
    #Traceback (most recent call last):
    # File "./atest", line 11, in <module>
    # acc_tkn=gen_ssn['access token']
    # KeyError: 'access token'
  • Balaganga
    is this, you are trying to convey:

    import logging
    from kiteconnect import KiteConnect
    logging.basicConfig(level=logging.DEBUG)
    key="xxxxxxxxxajl5"
    secret="xxxxxxxxxxxxxxxxxxxxxxdc"
    kite=KiteConnect(api_key=key)
    req_tkn=input("please input the request token and hit enter:")
    gen_ssn = kite.generate_session(req_tkn,api_secret=secret)
    print(gen_ssn)
    acc_tkn=gen_ssn['access_token']
    print(acc_tkn)

    Again the output is:


    please input the request token and hit enter:xxxxxxxxxxxxxxxxxxxxx0aErtROCQk7l


  • MAG
    @Balaganga Buddy, respectfully don't take the next sentence in the wrong way.
    But we are here to help but we cant do all your work for you and spoon feed you.

    Chuck out the sublime whatever and run the code at the command line using the syntax python <filename.py>
    You will see that it will print the out the contents of the gen_ssn variable.

    First of all you are running your code on windows. And on top of that you are using sublimetext's code execution tools to run the code. These add abstractions layers which dont really tell you whats going on.

    I tested the exact code at my end. replacing the api key, api secret and request token. And it worked. The difference was I ran it on linux and I ran it on the command line directly as python <filename.py>
  • Balaganga
    i am kid with respect to this area is concern, so i may need someone to spoon feed programming insight.
    anyway i lost 2k(for subscription of kiteconnect), not an issue.
    Thank you MAG for your correspondence.

    please someone help me to get access token.


  • MAG
    MAG edited December 2023
    The problem is without programming skills you cannot make much headway. I have already given you working code and all you had to do is replace the Api-key, api- secret and request token with your own values. And you are unable to get that simple 10 liner to work.
    Beyond this the only remaining option is to come to your home, sit at your keyboard and run the code for you.

    And this forum is not for teaching basics of programming. This is for people who want to use the API to develop code for trading. Average programming skills are a prerequisite.

    In which case you should go and learn programming first. You don't have to spend money either. There are so many free courses on the net.

    Here are links to some resources.
    How to learn Python programming | Guido van Rossum and Lex Fridman
    Learn Python - Full Course for Beginners [Tutorial]
    Harvard CS50’s Introduction to Programming with Python – Full University Course - This is a full fledged 16 hour course by David Malan who is considered one of the best lecturers at Havard.

    If you want more, all you need to do is go to youtube and search for "Python course"

    I would love to help you and I tried. But without a minimum proficiency in Python or any of the other supported programming languages, you are not going to make any progress. And no one can teach you programming using text chats on a forum like this one.
  • Balaganga
    Hi MAG,
    Yes, I understood.

    i got access token[ only mistake i did was path is not correct(in Command Prompt)]

    Thank you my friend.

  • MAG
    @Balaganga If you are planning to pursue this seriously I would suggest setting up a Linux VM using virtualbox and running all your code on Linux instead of windows. Will give much better performance and will save you from a lot of pain/headaches.
Sign In or Register to comment.