title: Why is my Poloniex lending bot failing to returnBalances?
body: <p>Recently, as a bit of a personal curiosity project, I have been writing an incredibly simple bot to use on Poloniex (a cryptocurrency exchange). I have been able to get the public API to work properly, but when I started testing the trading API, things started to fall apart. When I run this (and the 30 something other variations in an attempt to get things to work) all I get is
  <code>{"error":"Invalid command."}</code>. I am new to using basically every single one of the libraries I am using, so there's a huge margin for error that I can't narrow down.</p>
  
  <p>Poloniex API documentation is here: <a href="https://poloniex.com/support/api/" rel="nofollow noreferrer">https://poloniex.com/support/api/</a></p>
  
  <pre><code>public String returnBalances() {
      try {
          long nonce = System.nanoTime();
          String params = "command=returnBalances&amp;nonce=" + nonce;
  
          URL u = new URL(URL_PRIVATE + "?" + params);
  
          String sign = getSign(params);
  
          if(sign == null) return null;
  
          HttpURLConnection huc = (HttpURLConnection) u.openConnection();
          huc.setRequestMethod("GET");
          huc.setRequestProperty("Key", API_Key);
          huc.setRequestProperty("Sign", sign);
          huc.setRequestProperty("nonce", String.valueOf(nonce));
  
          return getDataFromHUC(huc);
      } catch (Exception e) {
          e.printStackTrace();
          return null;
      }
  }
  
  private String getSign(String c) {
      try {
          SecretKeySpec mac_key = new SecretKeySpec(API_secret.getBytes(), "HmacSHA512");
          Mac mac = Mac.getInstance("HmacSHA512");
          mac.init(mac_key);
          String sign = bytesToHex(mac.doFinal((c).getBytes()));
          return sign;
      }
      catch (Exception e) {
          return null;
      }
  }
  private String getDataFromHUC(HttpURLConnection huc) {
      try {
          huc.connect();
  
          BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));
          StringBuilder sb = new StringBuilder();
          String line;
          while ((line = br.readLine()) != null) {
              sb.append(line+"\n");
          }
          br.close();
          String data = sb.toString();
  
          return data;
      }
      catch (Exception e) {
          e.printStackTrace();
          return null;
      }
  }
  </code></pre>
  
  <p>Furthermore, while I was googling stuff to find an answer, I found that not a single person uses the <code>java.net</code> libraries (which I am). Have I completely missed base on what I should be doing? I would prefer to use these libraries if possible so I can really learn what's going on behind the scenes as much as I am able.</p>
  
  <p>Sorry if there's an obvious answer either here or somewhere else on this website. I've been at this for hours now with no progress so I'm bound to make mistakes.</p>
  
  <p>P.S. I am aware that my run-time error handling is probably pretty low quality, but I haven't had any problems with it so improving it is kind of a low priority.</p>