【问题标题】:HttpGet request to the Binance exchange error with javaHttpGet 请求 Binance 使用 java 交换错误
【发布时间】:2026-01-07 13:20:05
【问题描述】:

我在通过 Binance exchange 发送 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 长无

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":"参数'timestamp'中发现非法字符;合法范围是'^[0-9]{1,20}$'。"}

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

【问题讨论】:

  • 添加时间戳变量值示例
  • 时间戳 1499827319559 不起作用
  • 我建议你从 binance java API 开始,或者阅读他们建议的代码。

标签: java timestamp http-get


【解决方案1】:

你的网址是错误的。将?signature= 更改为&signature=

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      这篇文章真的很老了,但也许有人会帮助解决这个问题:

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

      【讨论】:

      • 嗨,我认为这个答案没有用。这个问题已经有了一个正确的、被接受的答案。问题是 URL 字符串中的一个简单拼写错误。公认的答案简短而甜蜜。这是您的选择,但我建议删除此答案。在我看来,它并没有为这个老问题增加任何东西。
      最近更新 更多