java 对 Binance 交易所错误的 HttpGet 请求

HttpGet request to the Binance exchange error with java

我在通过币安交易所发送 HTTP Get 请求时遇到问题。 (我需要 return 我的钱包状态)

GitHub 手册说 (https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md)


Account information (USER_DATA)

GET /api/v3/account (HMAC SHA256)

Get current account information.

Weight: 5

Parameters:

Name Type Mandatory Description

recvWindow LONG NO

timestamp LONG YES


我的代码如下

    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&timestamp=" + 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

服务器响应如下

{"code":-1100,"msg":"Illegal characters found in parameter 'timestamp'; legal range is '^[0-9]{1,20}$'."}

但是我的字符串时间戳是一个13位的数字字符串,应该没问题。请帮忙

你的url是错误的。将 ?signature= 更改为 &signature=.

您必须使用 & 作为 URL 中后续变量的分隔符。目前,?signature... 被视为 timestamp 变量的值,导致该错误消息。

查询字符串分隔符是 & 而不是 ?

使用:"https://api.binance.com/api/v3/account"+"?timestamp="+timestamp+"&signature="+sign

这个Post真的很老,但也许有人会帮助这个解决方案:

// 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&timestamp=" + 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);
}