Error while placing order

sudhanshugupta92
I have initialized KITE variable at a global level.

In my login function I have taken this KITE variable and after logging in, set the access token, My login function looks like this:
def login():
global KITE
try:
while True:
try:
KITE=KiteConnect(api_key=API_KEY)
print(KITE.login_url())
request_token=input("Enter the request_token after successfully logging in (Type exit to exit the program): ")
if (request_token.upper()=='EXIT'):
sys.exit()
else:
session_response=KITE.generate_session(request_token=request_token, api_secret=API_SECRET)
ACCESS_TOKEN=session_response["access_token"]
KITE.set_access_token(access_token=ACCESS_TOKEN)
print("\nLogin Successful\n")
return KITE, ACCESS_TOKEN
except Exception as e:
print("Invalid Token, try again: ",e)
traceback.print_exc()
sys.exit()
except:
print("Authentication Failed")
sys.exit()

My place_order function looks like this:
def place_buy_trade(variety,stock_symbol,quantity,transaction_type,order_type,product):
global KITE
try:
print("API Key==========================",KITE.api_key)
print("Access Token==========================",KITE.access_token) order_id=KITE.place_order(variety=variety,exchange="NSE",tradingsymbol=stock_symbol,transaction_type=transaction_type,quantity=quantity,product=product,order_type=order_type)
print(order_id)
return order_id
except Exception as e:
print("Error while placing order: ",e)
traceback.print_exc()
return 0

the indentation and other things being fine at my end, during runtime, I got this error:

--------------------------------------ERROR--------------------------------------------
Error while placing order: Invalid `api_key` or `access_token`.
Traceback (most recent call last):
File "E:\Sudhanshu\trader\algo_trader.py", line 39, in place_buy_trade
order_id=KITE.place_order(variety=variety,exchange="NSE",tradingsymbol=stock_symbol,transaction_type=transaction_type,quantity=quantity,product=product,order_type=order_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\parul\AppData\Local\Programs\Python\Python312\Lib\site-packages\kiteconnect\connect.py", line 361, in place_order
return self._post("order.place",
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\parul\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\parul\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.InputException: Invalid `api_key` or `access_token`.
-----------------------------------------ERROR-----------------------------------------------

Kindly suggest a way around, I have checked the API_KEY and ACCESS_TOKEN are being set correctly.
  • sujith
    It seems like you are not sending correct api_key or access_token.
    You can run with debug logs enabled for pykiteconnect. You can inspect the request and response json to know more.
Sign In or Register to comment.