I have to place orders and get streaming websockets data, "without" using client password. It should be possible to place orders as long as I have a valid token. But unfortunately, it is not working.
Steps:
- First I use authentication code to get various tokens:
import http.client
import mimetypes
import json
conn = http.client.HTTPSConnection(
"apiconnect.angelbroking.com"
)
payload = {
"clientcode":"Q45228106",
"password":"n6*4h257d"
}
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-UserType': 'USER',
'X-SourceID': 'WEB',
'X-ClientLocalIP': 'CLIENT_LOCAL_IP',
'X-ClientPublicIP': 'CLIENT_PUBLIC_IP',
'X-MACAddress': 'MAC_ADDRESS',
'X-PrivateKey': 'Buiajnbe'
}
conn.request(
"POST",
"/rest/auth/angelbroking/user/v1/loginByPassword",
json.dumps(payload),
headers
)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
This returns:
{
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"jwtToken": "eyJhbGciOiJIbh8sMiJ9.eyJ1c2VybmFtZSI6IlAyNTc5MTYiLCJyb2xlhn45hddXNlcnR5cGUiOiJVU0VSIiwiaWF0IjoxNjEwNjk0NTg2LCJleHAiOjE2MTA3MDM1ODZ9.kXwaoc_C6Dajr-RfqmH6nBjKOtUbx26LO5M469fQk-YPiae8aVchZYHPJuRQycNCdtSZOA9ysDVbqk-HHwQ5FQ",
"refreshToken": "eyJhbGcUzUxMiJ9.eyJ0b2tlbiI6IlJFRlJFU0gtVE9LRU4QiOjE2MTA2OTQ1ODZ9.NHOC-pm2bxdaO3f_tpF5hQ-nbOCYPSRZHqhIJA8xUEFSAw60kY5s90iDCunoSZ-Iy7j7UdEBCGIbjKQ",
"feedToken": "072820512"
}
}
- Now I should be able to use the tokens to:
- Place/edit/delete orders
- stream data using websocket
But neither of this happens.
Placing order
from smartapi import SmartConnect
obj=SmartConnect(api_key="Buiajnbe")
#login api call
CLIENT_ID = "P257916"
data = obj.generateSession(CLIENT_ID,"7Yh53h23K#")
refreshToken = data['data']['refreshToken']
# refreshToken = "eyJhbGcUzUxMiJ9.eyJ0b2tlbiI6IlJFRlJFU0gtVE9LRU4QiOjE2MTA2OTQ1ODZ9.NHOC-pm2bxdaO3f_tpF5hQ-nbOCYPSRZHqhIJA8xUEFSAw60kY5s90iDCunoSZ-Iy7j7UdEBCGIbjKQ"
#fetch the feedtoken
feedToken=obj.getfeedToken()
#fetch User Profile
userProfile= obj.getProfile(refreshToken)
#place order
try:
orderparams = {
"variety": "NORMAL",
"tradingsymbol": "RECLTD-EQ",
"symboltoken": "15355",
"transactiontype": "BUY",
"exchange": "NSE",
"ordertype": "LIMIT",
"producttype": "INTRADAY",
"duration": "DAY",
"price": "120",
"squareoff": "0",
"stoploss": "0",
"quantity": "1"
}
orderId=obj.placeOrder(orderparams)
print("The order id is: {}".format(orderId))
except Exception as e:
print("Order placement failed: {}".format(e.message))
The above code works, but it uses client id and password.
How do I use refresh token/ jwt token in this code?