3
votes

i am trying to send GET request to User Account Endpoint on Binance. although i am still getting Http Response 400 what am i doing wrong ? here is my code

public JSONObject getUserAccountData(String timeStamp ) {

    JSONObject json = null;
    try {
        String secret = apikey.get("secret");
        // https:///api.binance.com/api/v3/account
        String uri = ACCOUNT_URL_V3;
        String params = "recvWindow=" + DEFAULT_RECWINDOW + "&timestamp=" + timeStamp ;
        String fullUri = uri + "?" + params;
        map.put("signature", Encryptor.getHmacSha256(secret,params ));
        fullUri =fullUri+ "&signature="+ map.get("signature");

         String result = HTTPUtil.requestGet(fullUri , "application/json", map.get("X-MBX-APIKEY"));

        return json.getJSONObject("result");
    } catch (Exception ex) {
        logger.error("Failed getting user account data " + ex.getMessage());
    } finally {
        return json;
    }
}

I do sign my secret key with SHA256

public static String getHmacSha256(String key, String data) {
    Mac sha256;
    String result = null;

    try {
        byte[] byteKey = key.getBytes("UTF-8");
        sha256 = Mac.getInstance(HMAC_SHA256);
        SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA256);
        sha256.init(keySpec);
        byte[] macData = sha256.doFinal(data.getBytes("UTF-8"));
        result = bytesToHex(macData);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    return result;
}

Here is the GET implementation (works on Bitterex API calls)

public static String requestGet(String strUrl, String accept,String apiKeyForGet) throws Exception {
    URL url = new URL(strUrl);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

    conn.setDoOutput(true);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", accept);
     conn.setRequestProperty("X-MBX-APIKEY", apiKeyForGet);
    conn.setConnectTimeout(5000);
    conn.connect();

    if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
         throw new Exception();
    }

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    return response.toString();
}

The final URI I get is:
https://api.binance.com/api/v3/account?recvWindow=5000&timestamp=1523987972841&signature=3125BEE36896E464122A1CA73EB6FFA18881CB33FDE6927CC3676C8F969EC2A0

1

1 Answers

0
votes

Synchronise your timestamp with the following binance timestamp:

For me it was GMT + 2, So i have created make my Timestamp like - (3600 * 2)