Im trying to call "/api/v1/sub-accounts" endpoint to get Account balance but there is something wrong with it! here is my code :
public Observable<Response<String>> getBalance(){
long timestamp = Timestamp.from(Instant.now()).getTime() + localDelayTime;
return service.getBalance(
apiKey+"",
timestamp+"",
base64(encode(secKey,(timestamp+"GET"+"/api/v1/sub-accounts"+"")))+"",
base64(encode(secKey,passPhrase))+"",
"2")
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.retry(2)
.timeout(10,TimeUnit.SECONDS);
}
and my encryption methods are :
private String base64(String encode) {
try {
return Base64.getEncoder().encodeToString((encode).getBytes(StandardCharsets.UTF_8));
}catch (NullPointerException e){
return "";
}
}
and
public static String encode(String key, String data) {
Mac sha256;
try {
sha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256.init(secret_key);
return bytesToHex(sha256.doFinal(data.getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String bytesToHex(byte[] bytes) {
char[] HEX_ARRAY = "0123456789abcdef".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
the values of 'apikey , secretKey , passPhrase' are right also the value of timestamp , i checked it with server
This caught me offguard too. Passphrase is the API password, when you created the API at your account. Not the trading 6 digit code you use.