首页 > 解决方案 > HttpGet 请求 Binance 使用 java 交换错误

问题描述

我在通过 Binance 交换发送 HTTP Get 请求时遇到问题。(我需要返回我的钱包状态)

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


帐户信息 (USER_DATA)

获取 /api/v3/account (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&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 位数字字符串,应该没问题。请帮忙。

标签: javatimestamphttp-get

解决方案


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

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


推荐阅读