package com.angelbroking.smartapi.sample;
import com.angelbroking.smartapi.SmartConnect;
import com.angelbroking.smartapi.http.exceptions.SmartAPIException;
import com.angelbroking.smartapi.models.User;
import org.apache.commons.codec.binary.Base32;
import org.apache.commons.codec.binary.Hex;
import de.taimos.totp.TOTP;
import java.io.IOException;
public class LoginWithTOTPSample {
public static void main(String[] args) throws SmartAPIException, IOException {
String clientID = System.getProperty("clientID");
String clientPass = System.getProperty("clientPass");
String apiKey = System.getProperty("apiKey");
String secretKey = System.getProperty("secretKey");
SmartConnect smartConnect = new SmartConnect(apiKey);
String totp = getTOTP(secretKey);
User user = smartConnect.generateSession(clientID, clientPass, totp);
smartConnect.setUserId(user.userId);
smartConnect.setAccessToken(user.accessToken);
smartConnect.setRefreshToken(user.refreshToken);
Examples examples = new Examples();
System.out.println("getProfile");
examples.getProfile(smartConnect);
System.out.println("getLTP");
examples.getLTP(smartConnect);
System.out.println("Historic candle Data");
examples.getCandleData(smartConnect);
String feedToken = user.getFeedToken();
System.out.println("user.getFeedToken : " +feedToken);
String strWatchListScript = "nse_cm|2885&nse_cm|1594&nse_cm|11536&mcx_fo|221658";
String task = "mw";
examples.tickerUsage(clientID, feedToken, strWatchListScript, task);
}
public static String getTOTP(String secretKey) {
Base32 base32 = new Base32();
byte[] bytes = base32.decode(secretKey);
String hexKey = Hex.encodeHexString(bytes);
return TOTP.getOTP(hexKey);
}
}
/////////////////////////////////////////////////////////////////////////////////////
Output:
{"sourceID":"WEB","clientPublicIP":"xxx.xx.xxx.xxx","macAddress":"xx-xx-xx-xx-xx-xx","userType":"USER","clientLocalIP":"xxx.xxx.x.x","accept":"application/json"}***************************
{"data":{"feedToken":"xxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx-xxxxxx-xxx","jwtToken":"xxxxxxxxxx.xxxxxxxxxxxxxxx.xxxxxx-xxxxxxxxxxxx","refreshToken":"xxxxxxxxxx.xxxxxxxxxxx.xxxxxxxxxxxxxxx-xxxx"},"message":"SUCCESS","errorcode":"","status":true}***************************
getProfile
User [userName=xxxxxxxx, userId=xxxxxxxx, mobileNo=, brokerName=, email=, lastLoginTime=null, accessToken=null, refreshToken=null, products=[BO, NRML, CO, CNC, MIS, MARGIN], exchanges=[bse_cm, nse_cm, nse_fo], feedToken=null]
getLTP
{"high":595,"low":591.25,"tradingsymbol":"SBIN-EQ","ltp":594.4,"exchange":"NSE","close":598.15,"symboltoken":"3045","open":594}
Historic candle Data
{"data":[["2023-09-11T09:15:00+05:30",587,587,585.85,586.25,108082],["2023-09-11T09:16:00+05:30",586.3,586.5,585.55,586.35,94253],["2023-09-11T09:17:00+05:30",586.5,586.9,586.05,586.8,77669],["2023-09-11T09:18:00+05:30",586.55,587.35,586.55,587.35,58583],["2023-09-11T09:19:00+05:30",587.4,587.6,587.2,587.4,82521],["2023-09-11T09:20:00+05:30",587.35,587.5,586.8,587.45,52353]],"message":"SUCCESS","errorcode":"","status":true}
JSONObject["data"] not a string.
null
user.getFeedToken : xxxxxxxxxx.xxxxxxxxxxxxxxxxxx.xxxxxxxxxxx-xxxxxxxx-xxxx
com.neovisionaries.ws.client.WebSocketException: Failed to connect to 'wsfeeds.angelbroking.com:443': Connection timed out: connect
at com.neovisionaries.ws.client.SocketConnector.connectSocket(SocketConnector.java:127)
at com.neovisionaries.ws.client.SocketConnector.doConnect(SocketConnector.java:239)
at com.neovisionaries.ws.client.SocketConnector.connect(SocketConnector.java:190)
at com.neovisionaries.ws.client.WebSocket.connect(WebSocket.java:2351)
at com.angelbroking.smartapi.ticker.SmartAPITicker.connect(SmartAPITicker.java:288)
at com.angelbroking.smartapi.sample.Examples.tickerUsage(Examples.java:302)
at com.angelbroking.smartapi.sample.LoginWithTOTPSample.main(LoginWithTOTPSample.java:43)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:303)
at com.neovisionaries.ws.client.SocketInitiator$SocketRacer.run(SocketInitiator.java:126)
false
Process finished with exit code 0