Invalid session credentials

beast123
After getting the request token after the login flow I am using this code to get the access token .
But the code says Invalid session credentials . Where I am wrong ?

from kiteconnect import KiteConnect
import urllib.request
import webbrowser
#from pprint import pprint
#import requests



kite = KiteConnect(api_key="xxxxxx ")
#print(kite.login_url())
data = kite.request_access_token(request_token="zzzzzz",secret="yyyyyy")
kite.set_access_token(data["access_token"])

# Place an order
try:
order_id = kite.order_place(tradingsymbol="INFY",
exchange="NSE",
transaction_type="BUY",
quantity=1,
order_type="MARKET",
product="NRML")

print("Order placed. ID is", order_id)
except Exception as e:
print("Order placement failed", e.message)

# Fetch all orders
print(kite.orders())

please help me on this coz I am stuck . Thanks !
  • sujith
    @beast123,
    Things that you need to check, if you have this error.
    1. You should access API for same user with which you have registered app.
    2. Request token is valid for only 5 minutes, make sure you use it before it expires.
    3. Request token can be used only once, so make sure you don't call "request_access_token" API every time you run app. Once you get access token, store it and use it next time you run app.
  • beast123
    Is the code correct ?
    Can you please check ?
    its in python
  • beast123
    Basically this line of code

    data = kite.request_access_token(request_token="zzzzzz",secret="yyyyyy")
  • sujith
    Hi @beast123,
    Your login code is fine, but in place order method params, you should send "CNC" as product not "NRML". You should include "validity" as "DAY" also. You can checkout more examples here.
    But this has nothing to do with error thrown i.e. invalid credentials.

    PS: For equity product can be "CNC"(delivery) or "MIS"(intraday) whereas for futures, options, currency and commodity product can be "NRML" or "MIS".
  • beast123
    @sujith
    Because of this line I am getting an error ...It says invelid session credentials

    data = kite.request_access_token(request_token="zzzzzz",secret="yyyyyy")
  • sujith
    @beast123,
    It is because, request token can be used only once.
  • sujith
    sujith edited February 2017
    @beast123,
    You were able to access API on first run after giving new request token?
  • beast123
    I was able to print the url generated by the api . After that I opened the link in the browser and got the request token from the redirect url. Then I copied it and pasted it in the line above then it is giving me error . I just need to get format of the method above !
  • beast123
    @sujith No . It is giving an error in the first run also !
  • sujith
    @beast123,
    Can you private message your api_key?
Sign In or Register to comment.