0

Trying to send HTTP Post request to GDAX with auth. The 'GET' requests work fine and I'm unsure if I am passing the json params correctly as I keep getting Bad Request.

private static JsonObject LimitOrder(String symbol, boolean side, String quantity, String price)
    {
        BigDecimal dPrice = new BigDecimal(price);
        BigDecimal dQuantity = new BigDecimal(quantity);
        String sSide = side?"buy":"sell";
        String param  ="{\"size\":\""+dQuantity.toString()+"\",\"price\":\""+dPrice.toString()+"\",\"side\":\""+sSide+"\",\"product_id\":\""+symbol+"\",\"post_only\":true}";


        try
        {
                String timestamp= Instant.now().getEpochSecond()+"";
                String accessSign = getAccess(timestamp,"POST","/orders");
                String apiKey = properties.getProperty("key");
                String passphrase = properties.getProperty("passphrase");

                URL url = new URL("https://" + properties.getProperty("host") + "/orders");
                HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                connection.setDoOutput(true); // Triggers POST.
                connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                connection.setRequestProperty("CB-ACCESS-KEY", apiKey);
                connection.setRequestProperty("CB-ACCESS-SIGN", accessSign);
                connection.setRequestProperty("CB-ACCESS-PASSPHRASE", passphrase);
                connection.setRequestProperty("CB-ACCESS-TIMESTAMP", timestamp);
                connection.setRequestProperty("accept", "application/json");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);

        //      System.out.println("WRiting: " + param);
              try (OutputStream output = connection.getOutputStream()) {
                output.write(param.getBytes("UTF-8"));
              }

              String status = connection.getResponseMessage();
                System.out.println("STATUS: "+status);

                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuffer content = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                if(content.length()>0){
                System.out.println(content);
                }else{
                     System.out.println("Empty Response");

                }
                in.close();

                connection.disconnect();
            }catch(Exception e) {
                e.printStackTrace();
            }
        return null;
    }

and the access sign method below:

 private static String getAccess(String timestamp, String method, String path) throws NoSuchAlgorithmException, InvalidKeyException {
        String secret = properties.getProperty("secret");
        String prehash = timestamp+method+path;

        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        byte[] secretDecoded = Base64.getDecoder().decode(secret);
        SecretKeySpec secret_key = new SecretKeySpec(secretDecoded, "HmacSHA256");
        sha256_HMAC.init(secret_key);

        return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(prehash.getBytes()));
    }
0

1 Answer 1

0

Your JSON String seems fine, however I suggest you use JSONObject library class .

In your getAccess() method, I think you are missing the param variable.

   String prehash = timestamp + method + path + param;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.