Sunday, December 22, 2024

GDAX Java Restrict Order (HTTP Publish) – Bitcoin Stack Change

Making an attempt to ship HTTP Publish request to GDAX with auth. The ‘GET’ requests work superb and I am uncertain if I’m passing the json params appropriately as I preserve getting Unhealthy Request.

non-public static JsonObject LimitOrder(String image, boolean facet, String amount, String value)
    {
        BigDecimal dPrice = new BigDecimal(value);
        BigDecimal dQuantity = new BigDecimal(amount);
        String sSide = facet?"purchase":"promote";
        String param  ="{"measurement":""+dQuantity.toString()+"","value":""+dPrice.toString()+"","facet":""+sSide+"","product_id":""+image+"","post_only":true}";


        attempt
        {
                String timestamp= Instantaneous.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 material-Sort", "software/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("settle for", "software/json");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);

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

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

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

                }
                in.shut();

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

and the entry signal technique under:

 non-public static String getAccess(String timestamp, String technique, String path) throws NoSuchAlgorithmException, InvalidKeyException {
        String secret = properties.getProperty("secret");
        String prehash = timestamp+technique+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()));
    }

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles