Invalid session credentials

suraj
I am a newbie to kiteconnect. Here's my first code

from kiteconnect import KiteConnect
api_key ='XXXXXX'
secret ='XXXXX'
kite = KiteConnect(api_key)
request_token = kite.login_url()
print request_token
access_token = kite.request_access_token(request_token, secret)

And my code ends up giving this error.

File "build/bdist.linux-x86_64/egg/kiteconnect/__init__.py", line 208, in request_access_token

File "build/bdist.linux-x86_64/egg/kiteconnect/__init__.py", line 376, in _post

File "build/bdist.linux-x86_64/egg/kiteconnect/__init__.py", line 459, in _request

kiteconnect.exceptions.TokenException: Invalid session credentials

can anybody please tell me what's problem with my code and why am I getting this error?
  • suraj
    suraj edited March 2016
    Ok, I got to know that the problem I had was with getting request token. I need to redirect the login URL to get request token. I redirected the URL by doing

    response = requests.request('PUT', login_url,verify=False)

    Then I tried to parse the response. According to the document the response was supposed to be of content-type 'JSON' so I tried this

    json_data = json.loads(response.text)

    but my code ends up saying
    'ValueError: No JSON object could be decoded
    '
    Now I am unable to parse the response I am getting. Can anybody please help me with this?
    Can I get the sample code to parse the response and get request_token.
  • Kailash
    Response from the API is in JSON but once you use the pykiteconnect (which you are), the results are Python data structures (list, dict etc.). You don't need to use the JSON library,
  • suraj
    suraj edited March 2016
    Thanks for the Reply kailash.

    from kiteconnect import KiteConnect
    api_key ='XXXXXX'
    secret ='XXXXX'
    kite = KiteConnect(api_key)
    login_url = kite.login_url()
    request_token = ?
    access_token = kite.request_access_token(request_token, secret)

    Can you please tell me how to get request token using kiteconnect API?
  • Kailash
    Please read the documentation. The flow is explained (and graphically illustrated) well https://kite.trade/docs/connect/v1/#login-flow
  • suraj
    Thanks for the reply kailash.

    I have got my request token. Now my code looks like this

    from kiteconnect import KiteConnect
    api_key = 'XXXX'
    secret = 'YYYY'
    request_token = 'ZZZZ'
    kite = KiteConnect(api_key)
    access_token = kite.request_access_token(request_token, secret)

    When I run this. It ends up saying.

    access_token = kite.request_access_token(request_token, secret)
    File "build/bdist.linux-x86_64/egg/kiteconnect/__init__.py", line 208, in request_access_token

    File "build/bdist.linux-x86_64/egg/kiteconnect/__init__.py", line 376, in _post

    File "build/bdist.linux-x86_64/egg/kiteconnect/__init__.py", line 459, in _request

    kiteconnect.exceptions.TokenException: Invalid session credentials

    I am really not getting what's the problem now with my code. Can you please assist me with this? I am sure that all apikey,securitycode and request token I am using are right.
  • Kailash
    Can you please send an e-mail to [email protected] with the following details:
    1. api_key
    2. secret
    3. The request token you get after a login session
    4. The Zerodha user id you're logging in with
  • suraj
    Thanks for the reply kailash. I will mail you my credentials but before that I just want to know if the request token I got is right or not. The way I got my request token is by opening my login_url from unix command prompt by doing.

    curl -k https://kite.trade/connect/login?api_key=XXXXX

    It gave me 5 letters request token. I taught of posting the screenshot of the output I am unable to post the photo here on forum. I am mailing my credentials with that screenshot please check.
  • Kailash
    This is incorrect, Suraj. https://kite.trade/connect/login?api_key=XXXXX is the URL of the HTML page where the user should be redirected to login first (like a Facebook login). You only get the request_token after a successful login.
  • suraj
    I got to know. I opened the link in my browser and I got my request token which was long. I used that request token and the code is still giving the same error. I have mailed the new and right credentials. Please go through it.
  • ishandutta2007
    same problem. sent a mail too with details. waiting for reply!
  • Vivek
    @ishandutta2007 Check if your request_token is right and also you pasted right api secret of your app. As @Kailash explained in previous comments you will get request_token to your redirect url once you successfully authenticate and give permissions for app (first time login).
  • ishandutta2007
    This is how I get the request_token:

    kite = KiteConnect(api_key="my_apps_api_key")

    import requests
    url = kite.login_url()
    response = requests.get(url)
    redir_url = response.history[1].headers['Location']
    #Session id in redir_url is actually the request_token

    import re
    request_token_here = re.search("sess_id.*&",response.history[1].headers['Location']).group(0).strip('sess_id').strip('=').strip('&')
    I have verified in browser as well I get the same request_token.
    How do I confirm if login is succesful or failed, its redirecting to my given url in app.
    I have given Redirect URL *: as http://127.0.0.1
    In browser its redirecting there with a new request_token everytime.
    Does't it not mean its loggin in succesfully ?
  • Vivek
    Vivek edited October 2016
    @ishandutta2007 You are using sess_id which is not the request token. Open the redir_url in browser and complete the login process which will redirect you to the url which you have mentioned while creating the app.
  • ishandutta2007
    I opened in browser and copy pasted it,now I get this error:
    /anaconda/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py:791: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
    InsecureRequestWarning)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/anaconda/lib/python3.5/site-packages/kiteconnect/__init__.py", line 218, in request_access_token
    "checksum": checksum
    File "/anaconda/lib/python3.5/site-packages/kiteconnect/__init__.py", line 450, in _post
    return self._request(route, "POST", params)
    File "/anaconda/lib/python3.5/site-packages/kiteconnect/__init__.py", line 533, in _request
    raise(exp(data["message"], code=r.status_code))
    kiteconnect.exceptions.PermissionException: That API call is not allowed for the authenticated user
    Does it mean login is not succeeding yet I am getting a request_token?

    This is the redirected url looks like this:
    http://127.0.0.1/?status=success&request_token=abcdef8x0mmj7fa3ta10mdhs56hnp1xyz
  • ishandutta2007
    ishandutta2007 edited October 2016
    Found my problem, I have created a Publisher app, need to create a Connect app.
    But does it mean as soon as I create a connect app for testing/development I start getting billed.
    I don't plan to use the connect app in production now but just build the app so that I can use later, can the billing start from month when I start using and let me build without having to worry about getting billed right away?
  • Vivek
    @ishandutta2007 You have to pay to get started with Kite Connect api.
  • ishandutta2007
    ishandutta2007 edited October 2016
    How is it billed ? If I pay 2000 now, can I use it till 25th Nov and then deactivate such that I dont have to pay further ?
  • Kailash
    @ishandutta2007 Yes, that is correct. That said, we've been working on building a free sandbox environment, but it's still sometime away.
Sign In or Register to comment.