我无法通过Binance交换发送HTTP Get请求。 (我需要返回我的钱包状态)
GitHub手册说(https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md)
帐户信息(USER_DATA)
获取/ api / v3 /帐户(HMAC SHA256)
获取当前帐户信息。
重量:5
参数:
名称类型强制说明
recvWindow长否
时间戳很长
我的代码如下所示
public static String timestamp = String.valueOf(System.currentTimeMillis());
public static void wallet_status () throws NoSuchAlgorithmException, InvalidKeyException {
String url = "https://api.binance.com/api/v3/account×tamp=" + timestamp;
//sign url
Mac shaMac = Mac.getInstance("HmacSHA256");
SecretKeySpec keySpec = new SecretKeySpec(BINANCE_SECRET_KEY.getBytes(), "HmacSHA256");
shaMac.init(keySpec);
final byte[] macData = shaMac.doFinal(url.getBytes());
String sign = Hex.encodeHexString(macData);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://api.binance.com/api/v3/account"+"?timestamp="+timestamp+"?signature="+sign);
request.addHeader("X-MBX-APIKEY", BINANCE_API_KEY);
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
try (InputStream stream = entity.getContent()) {
BufferedReader reader =
new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
} //end
服务器响应如下
{“代码”:-1100,“ msg”:“在参数'时间戳'中发现非法字符;合法范围为'^ [0-9] {1,20} $'。”}
但是我的String时间戳是一个13位的数字字符串,应该没问题。请帮忙。
答案 0 :(得分:1)
您的网址是错误的。将?signature=
更改为&signature=
。
您必须使用&
作为URL中后续变量的分隔符。当前,?signature...
被视为timestamp
变量的值,从而导致该错误消息。
答案 1 :(得分:1)
查询字符串分隔符为&
而不是?
使用:"https://api.binance.com/api/v3/account"+"?timestamp="+timestamp+"&signature="+sign
答案 2 :(得分:-1)
这篇文章真的很旧,但也许有人会帮助解决这个问题:
// Binance testnet Data
private String baseUrl = "https://api.binance.com";
private String apiKey = "you API Key";
private String apiSecret = "Your Secret";
private String endpoint = "/api/v3/account";
private String parameters = "recvWindow=20000×tamp=" + System.currentTimeMillis();
public void getData() throws Exception {
byte[] bytes = hmac("HmacSHA256", apiSecret.getBytes(), parameters.getBytes());
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(baseUrl + endpoint + "?" + parameters + "&signature=" + bytesToHex(bytes)))
.setHeader("X-MBX-APIKEY", apiKey)
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
// print status code
System.out.println(response.statusCode());
// print response body
System.out.println(response.body());
}
public static byte[] hmac(String algorithm, byte[] key, byte[] message) throws Exception {
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(message);
}
public static String bytesToHex(byte[] bytes) {
final char[] hexArray = "0123456789abcdef".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0, v; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}