2

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.

Community
  • 1
  • 1
Stephen Buttolph
  • 643
  • 8
  • 16

1 Answers1

0

You need to hex your sign before put into http header.

String sign = org.apache.commons.codec.binary.Hex.encodeHexString(digest);
sendon1982
  • 9,982
  • 61
  • 44
  • Well I tried to put this line and Android Studio marks it in red. Also it doesnt allow to import org.apache.commons.codec.binary.Hex. – wilkas Nov 01 '17 at 04:38
  • I think you need to add dependency on that artifact. For example in Maven pom xml: commons-codec commons-codec ${commons.codec.version} But I do not know how to do it in Android. – sendon1982 Nov 03 '17 at 08:52