I have been working on a currency trading program. This program is based off of the Bittrex API. However, I have encountered a problem translating the php code:
$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
Into Java code. Here's what I have so far:
String apikey = "xxx";
String apisecret = "xxx";
String nonce = String.valueOf(System.currentTimeMillis());
String uri = "https://bittrex.com/api/v1.1/market/getopenorders?apikey=" + apikey + "&nonce=" + nonce;
Mac mac = Mac.getInstance("HmacSHA512");
SecretKeySpec secret = new SecretKeySpec(apisecret.getBytes(),"HmacSHA512");
mac.init(secret);
byte[] digest = mac.doFinal(uri.getBytes());
String sign = new String(digest);
HttpURLConnection con = (HttpURLConnection) new URL(uri).openConnection();
con.setRequestProperty("apisign", sign); // << very confused
con.setRequestMethod("GET");
con.connect();
con.getInputStream(); // << Exception is thrown
I'm getting the error: Server returned HTTP response code: 400. So I know I'm messing up the data I'm sending to the server. I think the problem is with the last "paragraph" of code because I tried to interpret code from this question. Also, the reasoning behind the second "paragraph" of code was born here. I am then planning on reading the response as explained here.