简体   繁体   中英

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. Here is the documentation page: 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 . I can do a general request to Binance, but I cannot get my private data, using my private keys.

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

And I pass as a header as Danny suggested the 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. Another easy fix is to use the ccxt library: 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

API-keys are passed into the Rest API via the X-MBX-APIKEY header.

In your Request, add that as the header key and your API Key as the value.

Get the official Postman collections for the Binance API from here:

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

Add your API key to the binance-api-key environment variable and your secret key to the binance-api-secret variable.

CAUTION: Limit what the key can do in Binance key management. 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

You need to retrieve serverTime time from https://api.binance.com/api/v3/time and need to use that serverTime to sign the request.

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

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

what i have is this, but objHTTP.setRequestHeader giveme "no argument found"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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