【发布时间】:2015-10-31 13:56:01
【问题描述】:
我一直在从事货币交易计划。该程序基于Bittrex API。但是,我在翻译php代码时遇到了问题:
$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);
进入 Java 代码。到目前为止,这是我所拥有的:
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
我收到错误消息:服务器返回 HTTP 响应代码:400。所以我知道我将发送到服务器的数据弄乱了。我认为问题在于代码的最后一个“段落”,因为我试图解释来自this question 的代码。此外,代码的第二“段”背后的推理诞生于here。然后,我计划按照here 的说明阅读回复。
【问题讨论】: