简体   繁体   English

Postman 对币安的 GET 请求 API

[英]Postman GET request to Binance API

I'm trying to send a GET request to Binance's API, but I don't exactly know how to.我正在尝试向 Binance 的 API 发送GET请求,但我不知道该怎么做。 Here is the documentation page: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data这是文档页面: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data

I have a private apiKey and secretKey .我有一个私人apiKeysecretKey I can do a general request to Binance, but I cannot get my private data, using my private keys.我可以向 Binance 提出一般性请求,但我无法使用我的私钥获取我的私人数据。

First try: For the GET request in Postman I use this string: https://api.binance.com/api/v3/account?timestamp=1499827319559&signature= here_I_put_my_secret_key第一次尝试:对于 Postman 中的 GET 请求,我使用以下字符串: https://api.binance.com/api/v3/account?timestamp=1499827319559&signature= here_I_put_my_secret_key

And I pass as a header as Danny suggested the apiKey .我通过header作为 Danny 建议的apiKey

But I get:但我得到:

    {
    "code": -1021,
    "msg": "Timestamp for this request is outside of the recvWindow."
    }

Thanks.谢谢。

I solved this correcting the time using javascript in Postman.我在 Postman 中使用javascript解决了这个校正时间问题。 Another easy fix is to use the ccxt library: https://github.com/ccxt/ccxt另一个简单的解决方法是使用ccxt库: https://github.com/ccxt/ccxt

This might be what you're after, as per the documentation.根据文档,这可能是您所追求的。

https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#endpoint-security-type https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#endpoint-security-type

API-keys are passed into the Rest API via the X-MBX-APIKEY header. API 密钥通过X-MBX-APIKEY header 传递到 Rest API。

In your Request, add that as the header key and your API Key as the value.在您的请求中,将其添加为 header 密钥和您的 API 密钥作为值。

Get the official Postman collections for the Binance API from here:从这里获取 Binance API 的官方 Postman collections:

https://github.com/binance/binance-api-postman https://github.com/binance/binance-api-postman

Import the desired collection and environment in Postman, for instance binance_spot_api_v1.postman_collection.json and binance_com_spot_api.postman_environment.json在 Postman 中导入所需的集合和环境,例如binance_spot_api_v1.postman_collection.jsonbinance_com_spot_api.postman_environment.Z466DEEC76ECDF5FCA6D38571F623

Add your API key to the binance-api-key environment variable and your secret key to the binance-api-secret variable.将您的 API 密钥添加到binance-api-key环境变量,并将您的密钥添加到binance-api-secret变量。

CAUTION: Limit what the key can do in Binance key management.注意:限制密钥在 Binance 密钥管理中的功能。 Do not use this key for production, only for testing.不要将此密钥用于生产,仅用于测试。 Create new key for production.为生产创建新密钥。

For the signed requests calculate the signature in a Pre-request Script then set the signature environment variable.对于已签名的请求,计算预请求脚本中的签名,然后设置签名环境变量。

Example Pre-request Script:预请求脚本示例:

function resolveQueryString() {
  const query = JSON.parse(JSON.stringify(pm.request.url.query)) 
  const keyPairs = []
  for (param of query) {
    if (param.key === 'signature') continue
    if (param.disabled) continue
    if (param.value === null) continue
    const value = param.value.includes('{{') ? pm.environment.get(param.key) : param.value
    keyPairs.push(`${param.key}=${value}`)
  }
  return keyPairs.join('&')
}

const signature = CryptoJS.HmacSHA256(
  resolveQueryString(),
  pm.environment.get('binance-api-secret')
).toString(CryptoJS.enc.Hex)
pm.environment.set('signature', signature)

You can try this.你可以试试这个。 This is working for me.这对我有用。 Just Replace your API_KEY and SECRET只需替换您的 API_KEY 和 SECRET

You need to retrieve serverTime time from https://api.binance.com/api/v3/time and need to use that serverTime to sign the request.您需要从https://api.binance.com/api/v3/time检索 serverTime 时间,并且需要使用该 serverTime 来签署请求。

GET : https://api.binance.com/api/v3/account?timestamp={{timestamp}}&signature={{signature}}

Header: Header:

Content-Type:application/json
X-MBX-APIKEY:YOUR_API_KEY

Pre-request Script:预请求脚本:

pm.sendRequest('https://api.binance.com/api/v3/time', function (err, res) {
        console.log('Timestamp Response: '+res.json().serverTime);
        pm.expect(err).to.not.be.ok;
        var timestamp = res.json().serverTime;

        postman.setEnvironmentVariable('timestamp',timestamp)  
        postman.setGlobalVariable('timestamp',timestamp) 

        let paramsObject = {};

        const binance_api_secret = 'YOUR_API_SECRET';

        const parameters = pm.request.url.query;

        parameters.map((param) => {
            if (param.key != 'signature' && 
                param.key != 'timestamp' && 
                !is_empty(param.value) &&
                !is_disabled(param.disabled)) {
                    paramsObject[param.key] = param.value;
            }
        })
        
        Object.assign(paramsObject, {'timestamp': timestamp});

        if (binance_api_secret) {
            const queryString = Object.keys(paramsObject).map((key) => {
                return `${encodeURIComponent(key)}=${paramsObject[key]}`;
            }).join('&');
            console.log(queryString);
            const signature = CryptoJS.HmacSHA256(queryString, binance_api_secret).toString();
            pm.environment.set("signature", signature);
        }

        function is_disabled(str) {
            return str == true;
        }

        function is_empty(str) {
            if (typeof str == 'undefined' ||
                !str || 
                str.length === 0 || 
                str === "" ||
                !/[^\s]/.test(str) ||
                /^\s*$/.test(str) ||
                str.replace(/\s/g,"") === "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
); 

I need to put my api-key in the header but I can't understand how I'm doing it in excel VBA if you can help me I appreciate it我需要将我的 api-key 放入 header 但我不明白我是如何在 excel VBA 中做的,如果你能帮助我,我很感激

what i have is this, but objHTTP.setRequestHeader giveme "no argument found"我所拥有的是这个,但 objHTTP.setRequestHeader 给我“找不到参数”

Sub Test19()
    Dim strResult As String
    Dim objHTTP As Object
    Dim URL, key As String
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "https://fapi.binance.com/fapi/v1/openOrder?symbol=BTCBUSD&timestamp=1643062898421&signature=__MYSIGNATURE__"
    objHTTP.Open "GET", URL, False
    'objHTTP.setRequestHeader "X-MBX-APIKEY: __MY_API_KEY"
    objHTTP.send
    objHTTP.waitForResponse
    strResult = objHTTP.responseText
    MsgBox strResult
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM