0
votes

What am I doing wrong?

I'm trying to get info of my account but something is not correct.

var apiKey = 'myAPIKey';
var secret = 'mySecret';
var queryString = 'timestamp=' + Date.now();
var hash = CryptoJS.HmacSHA256(queryString,secret);
var url = 'https://api.binance.com/api/v3/account?'+ queryString + '&signature=' +hash;

$('.botonOrden').click(function(){
    $.ajax({
        beforeSend: function(req){
            req.setRequestHeader("X-MBX-APIKEY", apiKey);
        },
        type: 'GET',
        url: url
    })
    .done(function() {
        console.log("success");
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
});

The console says:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.binance.com/api/v3/account?timestamp=1522383455036&signature=MyHash.
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

I'm think I'm doing it as shown in the doc: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#signed-endpoint-examples-for-post-apiv1order

https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data

1
I don't get Cross-Origin Request Blocked error, i get 404 error:codepen.io/spmsupun/pen/mxxRro - Supun Praneeth
you need to open a binance account and put your apiKey and Secret in the variables var apiKey; and var secret; and then it send a GET request with correct url - Bravo Vlog Hector Bravo
even that it shouldn't get 404 error, it should show invalid key error.try with invalid key and see what you get - Supun Praneeth
you right, my mistake, actually I update the code to make me understand better: - Bravo Vlog Hector Bravo
$('.botonOrden').click(function(){ var invocation = new XMLHttpRequest(); var url = 'api.binance.com/api/v1/time'; callOtherDomain(); function callOtherDomain(){ if(invocation) { invocation.open('GET', url, true); invocation.send(); } } }); - Bravo Vlog Hector Bravo

1 Answers

0
votes

I encountered 400 errors with the same 'account' API call as you did.

The solution I found was to request a timestamp from the Binance server (using /api/v1/time) and not generate it myself.

Secondly I had to add &recvWindow=5000 to the query string next to the timestamp.

The recvWindow is in the Binance documentation an optional parameter, but I learned the hard way that what is written is not always true… The recVWindow made it work.

I hope this helps resolving the issue.