我正在尝试向Binance上的用户帐户端点发送GET请求。 虽然我仍然得到Http Response 400 我究竟做错了什么 ? 这是我的代码
public JSONObject getUserAccountData(String timeStamp ) {
JSONObject json = null;
try {
String secret = apikey.get("secret");
// https:///api.binance.com/api/v3/account
String uri = ACCOUNT_URL_V3;
String params = "recvWindow=" + DEFAULT_RECWINDOW + "×tamp=" + timeStamp ;
String fullUri = uri + "?" + params;
map.put("signature", Encryptor.getHmacSha256(secret,params ));
fullUri =fullUri+ "&signature="+ map.get("signature");
String result = HTTPUtil.requestGet(fullUri , "application/json", map.get("X-MBX-APIKEY"));
return json.getJSONObject("result");
} catch (Exception ex) {
logger.error("Failed getting user account data " + ex.getMessage());
} finally {
return json;
}
}
我用SHA256
签署我的密钥public static String getHmacSha256(String key, String data) {
Mac sha256;
String result = null;
try {
byte[] byteKey = key.getBytes("UTF-8");
sha256 = Mac.getInstance(HMAC_SHA256);
SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA256);
sha256.init(keySpec);
byte[] macData = sha256.doFinal(data.getBytes("UTF-8"));
result = bytesToHex(macData);
}
catch(Exception e) {
e.printStackTrace();
}
return result;
}
这是GET实现(适用于Bitterex API调用)
public static String requestGet(String strUrl, String accept,String apiKeyForGet) throws Exception {
URL url = new URL(strUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", accept);
conn.setRequestProperty("X-MBX-APIKEY", apiKeyForGet);
conn.setConnectTimeout(5000);
conn.connect();
if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new Exception();
}
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
答案 0 :(得分:0)
将时间戳与以下币安时间戳同步:
对我来说,这是格林尼治标准时间+ 2,所以我创建了使我的时间戳类似-(3600 * 2)