Binance API密钥

时间:2017-12-26 23:10:56

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

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

  

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

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

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

7 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

X-MBX-APIKEY应设置为HTTP标头中的字段,而不是HTTP参数。有关HTTP标头字段的详细信息,请参阅this page。 但是,我尝试使用Excel,直到现在才能运行它。

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

答案 2 :(得分:0)

这对我有用:

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

答案 3 :(得分:0)

Binance的websocket API有点难以使用。此外,没有办法使用密钥。

常用用法

  1. 将带有您的秘密API密钥的HTTP POST请求作为X-MBX-APIKEY标题发送到https://api.binance.com/api/v1/userDataStream
  2. 您将获得应该用于websocket连接的listen键。它将提供1小时。
      

    {“listenKey”:“你的听键在这里”}

  3. 连接到Binance的websocket时使用它
  4. 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})
    

答案 4 :(得分:0)

您将其放在标题中。以下是从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);

答案 5 :(得分:0)

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()

答案 6 :(得分:-3)

卷曲-H“ X-MBX-APIKEY:your_api_key” -X POST https://api.binance.com/api/v1/userDataStream