Binance signature error : "code":-1022,"msg":"Signature for this request is not valid."

Viewed 5001

I have an issue with binance API signature for their REST API.

When trying to hit the route 'http://binance.com/api/v3/account', I get the following error:

{"code":-1022,"msg":"Signature for this request is not valid."}

I use nodejs and express.

I've seen there is a few questions on this subject but none seems to solve my problem so:

I define keys and urls there

const binanceConfig = {
    API_URL: 'http://binance.com',
    API_ENDPOINT: '/api/v3/account',
    API_KEY: 'API_KEY_EXAMPLE',
    API_SECRET: 'API_SECRET_EXAMPLE'
}

I create the signature

function generateSignature() {

    const dataQueryString = "recvWindow=60000&timestamp=" + Date.now();
    return crypto
        .createHmac('sha256', binanceConfig['API_SECRET'])
        .update(dataQueryString)
        .digest('hex'); 
}

I define query parameters here

const queryParameters = {
    timestamp: Date.now(),
    signature: generateSignature(),
    recvWindow: '60000',
}

Set the header

var header = {
    'Accept': 'Application/json',
    'X-MBX-APIKEY': binanceConfig['API_KEY']
};

Create the route to call the API

router.get('/userInfo', (req, res) => {
    var stringTest = `timestamp=${queryParameters['timestamp']}`

    requestUrl = binanceConfig['API_URL'] + binanceConfig['API_ENDPOINT'] + "?" + stringTest + "&" + "signature=" + queryParameters['signature'] + "&recvWindow=" + queryParameters['recvWindow'];

    const options = {
        url: requestUrl,
        headers: header,
        method: 'GET'
    }

    request(options, (error, response) => {
        if (error) { console.log('ERROR'); } 
        console.log(`Response: ${response.statusCode}`);
        console.log(response.body);
    });
});

If anyone has any idea why I get this error I'd be gratefull ! Thanks !

1 Answers

You need to have the timestamp and signature (respectively) as the last parameters.

Source

Related