Tuesday, November 26, 2024
HomeBitcoinGDAX Java Restrict Order (HTTP Put up) - Bitcoin Stack Alternate

GDAX Java Restrict Order (HTTP Put up) – Bitcoin Stack Alternate


Making an attempt to ship HTTP Put up request to GDAX with auth. The ‘GET’ requests work effective and I am not sure if I’m passing the json params accurately as I hold getting Dangerous Request.

non-public static JsonObject LimitOrder(String image, boolean aspect, String amount, String worth)
    {
        BigDecimal dPrice = new BigDecimal(worth);
        BigDecimal dQuantity = new BigDecimal(amount);
        String sSide = aspect?"purchase":"promote";
        String param  ="{"dimension":""+dQuantity.toString()+"","worth":""+dPrice.toString()+"","aspect":""+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-Kind", "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 methodology beneath:

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

Most Popular

Recent Comments