簡體   English   中英

使用java向Binance交換錯誤的HttpGet請求

[英]HttpGet request to the Binance exchange error with java

我在通過幣安交易所發送 HTTP Get 請求時遇到問題。 (我需要返回我的錢包狀態)

GitHub 手冊說( https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md


帳戶信息 (USER_DATA)

GET /api/v3/account (HMAC SHA256)

獲取當前帳戶信息。

重量:5

參數:

名稱 類型 必填 說明

recvWindow LONG NO

時間戳 長 是


我的代碼如下所示

    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":"在參數'timestamp'中發現非法字符;合法范圍是'^[0-9]{1,20}$'。"}

但我的字符串時間戳是一個 13 位數字字符串,應該沒問題。 請幫忙。

你的網址不對。 ?signature=更改為&signature=

您必須使用&作為 URL 中后續變量的分隔符。 目前, ?signature...被視為timestamp變量的值,導致該錯誤消息。

查詢字符串分隔符是&不是?

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

這篇文章真的很舊,但也許有人會幫助解決這個問題:

// 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM