【问题标题】:Binance API Keys币安 API 密钥
【发布时间】:2018-06-07 15:42:23
【问题描述】:

我在 Binance 上设置了一个只读 API 密钥来访问货币余额等账户信息,但我看不到 JSON 数据。我在 URL 中输入的字符串查询返回以下错误:

{"code":-2014,"msg":"API-key 格式无效。"}

我使用的网址是:https://api.binance.com/api/v3/account?X-MBX-APIKEY=**key**&signature=**s-key**

币安 API 的文档可以在这里找到:https://www.binance.com/restapipub.html。我究竟做错了什么 ?

【问题讨论】:

  • 我也想连接到币安。目前尚不清楚如何从文档中执行此操作,但此外,我什至无法创建 API 密钥。当我进入创建 API 密钥的屏幕时,单击创建密钥的按钮不会执行任何操作。

标签: api authentication url httprequest pre-signed-url


【解决方案1】:

你把它放在标题中。以下是从 jaggedsoft binance PHP 库借来的经过测试的工作 PHP 示例,它是一个签名请求,将返回帐户状态。

$api_key = "cool_key";
$secret = "awesome_secret";

$opt = [
  "http" => [
    "method" => "GET",
    "header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$api_key}\r\n"
  ]
];
$context = stream_context_create($opt);
$params['timestamp'] = number_format(microtime(true)*1000,0,'.','');
$query = http_build_query($params, '', '&');
$signature = hash_hmac('sha256', $query, $secret);
$endpoint = "https://api.binance.com/wapi/v3/accountStatus.html?{$query}&signature={$signature}";

$res = json_decode(file_get_contents($endpoint, false, $context), true);

【讨论】:

    【解决方案2】:
    def get_listen_key_by_REST(binance_api_key):
        url = 'https://api.binance.com/api/v1/userDataStream'
        response = requests.post(url, headers={'X-MBX-APIKEY': binance_api_key})  # ['listenKey']
        json = response.json()
        return json['listenKey']
    
    
    print(get_listen_key_by_REST(binance_api_key))
    
    
    def get_all_orders(symbol, binance_api_key, binance_secret_key):
        """Get all account orders; active, canceled, or filled.
        Args:   symbol: Symbol name, e.g. `BTCUSDT`.
        Returns:
        """
    
        from datetime import datetime, timezone, timedelta
        now = datetime.now(timezone.utc)
        epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)  # use POSIX epoch
        posix_timestamp_micros = (now - epoch) // timedelta(microseconds=1)
        posix_timestamp_millis = posix_timestamp_micros // 1000  # or `/ 1e3` for float
    
        import hmac, hashlib
        queryString = "symbol=" + symbol + "&timestamp=" + str(
            posix_timestamp_millis)
        signature = hmac.new(binance_secret_key.encode(), queryString.encode(), hashlib.sha256).hexdigest()
        url = "https://api.binance.com/api/v3/allOrders"
        url = url + f"?{queryString}&signature={signature}"
        response = requests.get(url, headers={'X-MBX-APIKEY': binance_api_key})
        return response.json()
    

    【讨论】:

      【解决方案3】:

      curl -H "X-MBX-APIKEY:your_api_key" -X POST https://api.binance.com/api/v1/userDataStream

      【讨论】:

      • 这并没有添加其他答案已经提到的任何内容。
      【解决方案4】:

      Binance 的 websocket API 使用起来有点棘手。也没有办法使用密钥。

      常见用法

      1. 使用您的 API 密钥作为 X-MBX-APIKEY 标头向 https://api.binance.com/api/v1/userDataStream 发送 HTTP POST 请求
      2. 您将获得用于 websocket 连接的监听键。它将在 1 小时后可用。

        {"listenKey": "你的监听键"}

      3. 连接到币安的 websocket 时使用它

      wss://stream.binance.com:9443/ws/{your listen key here}

      Python 示例

      import ssl
      from websocket import create_connection
      
      import requests
      
      KEY = 'your-secret-key'
      url = 'https://api.binance.com/api/v1/userDataStream'
      listen_key = requests.post(url, headers={'X-MBX-APIKEY': KEY})['listenKey']
      connection = create_connection('wss://stream.binance.com:9443/ws/{}'.format(KEY), 
                                     sslopt={'cert_reqs': ssl.CERT_NONE})
      

      【讨论】:

      • 这是正确的,除了您必须在 X-MBX-APIKEY 标头中发送 API 密钥而不是 API 机密,不需要签名。
      【解决方案5】:

      这对我有用:

      base_url="https://api.binance.com"
      account_info="/api/v3/account"
      
      url="${base_url}${account_info}"
      
      apikey="your_apikey"
      secret="your_secret"
      
      queryString="timestamp=$(date +%s)" #$(python3 binance_time.py) must sync
      requestBody=""
      
      signature="$(echo -n "${queryString}${requestBody}" | openssl dgst -sha256 -hmac $secret)"
      signature="$(echo $signature | cut -f2 -d" ")"
      
      req=$(curl -H "X-MBX-APIKEY: $apikey" -X GET "$url?$queryString&signature=$signature")
      echo $req
      

      【讨论】:

      • 是什么语言?
      【解决方案6】:

      X-MBX-APIKEY 应设置为 HTTP 标头中的字段,而不是 HTTP 参数。有关 HTTP 标头字段的更多信息,请参阅 this page。 但是,我在 Excel 上进行了同样的尝试,直到现在才运行它。

      另一个悬而未决的问题是如何使用密钥。

      【讨论】:

        【解决方案7】:

        您应该在请求标头中设置 API 密钥,而不是作为请求 url 中的参数。请提供有关您的请求程序的更多信息(语言等)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-12
          • 2020-04-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-02
          • 1970-01-01
          • 2016-10-06
          相关资源
          最近更新 更多