'Issue With Binance API Fetch In Chrome
My Code Works Fine In anWriter App But Doesn't Work In Chrome Browser !
It Freezes After Reaching The Fetch Line In Account_Fetch Function !
If Code Works Fine It Will Show OK Message !
Documentation :
Note : This Account Is For Test And That's Why I Share My API Key And Secret Key !
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.js"></script>
<script>
let AccessKey="1q02kgTluaKFYH9vgBAQyIIIN7UmSSCZvI8dELzM4RsNR3WEWJJqb6cIdaPVkYjE";
let SecretKey="IFcsWGidd6WQFZMKlUMd9fzTn0ztBV8esRl1BSz5O9vrKClrlDXorVAxUxJGWkwk";
let ServerTime=0;
let MaxDelay=60000;
Process();
async function Process()
{
await Time_Fetch();
await Account_Fetch(AccessKey,SecretKey,ServerTime,MaxDelay);
alert("OK");
}
async function Time_Fetch()
{
let URL="https://api.binance.com/api/v3/time";
let Request=URL;
let Fetch=await fetch(Request);
let JSON=await Fetch.json();
let Data=await JSON;
ServerTime=parseInt(Data.serverTime);
}
async function Account_Fetch(AccessKey,SecretKey,ServerTime,MaxDelay)
{
let URL="https://api.binance.com/api/v3/account";
let Parameters="timestamp="+ServerTime+"&"+"recvWindow="+MaxDelay;
let Signature=CryptoJS.HmacSHA256(Parameters,SecretKey);
let Request=URL+"?"+Parameters+"&"+"signature="+Signature;
let Fetch=await fetch(Request,{method:"get",headers:{"X-MBX-APIKEY":AccessKey}});
let JSON=await Fetch.json();
let Data=await JSON;
}
</script>
</html>
Solution 1:[1]
Have a look at the following code - this one runs to the end. However i was toled that that "you can't make signed API requests from a browser"
async function Process()
{
await Time_Fetch();
await Account_Fetch(AccessKey,SecretKey,ServerTime,MaxDelay);
alert("OK");
}
async function Time_Fetch()
{
let URL="https://api.binance.com/api/v3/time";
let Request=URL;
let response=await fetch(Request)
.then(blob => blob.json)
ServerTime=parseInt(Blob.serverTime);
}
async function Account_Fetch(AccessKey,SecretKey,ServerTime,MaxDelay)
{
let URL="https://api.binance.com/api/v3/account";
let Parameters="timestamp="+ServerTime+"&"+"recvWindow="+MaxDelay;
let Signature=CryptoJS.HmacSHA256(Parameters,SecretKey);
let Request=URL+"?"+Parameters+"&"+"signature="+Signature;
let Fetch=await fetch(Request,{method:"get",headers:{"X-MBX-APIKEY":AccessKey}, mode: 'no-cors'})
.then(blob => blob.json)
}
</script>
</html>
Solution 2:[2]
Everything looks good except you're missing a toString()
at the end of HmacSHA256 method.
let Signature = CryptoJS.HmacSHA256(Parameters,SecretKey).toString();
this method return an object like this one (for example):
{words: [8], sigBytes: 32}
words: [8]
0: 670167524
1: 2092892393
2: 560578323
3: 567480325
4: -1211153183
5: -1038754858
6: 926457206
7: 683147716
sigBytes: 32
So, you need to convert it to string.
Solution 3:[3]
and need to change ServerTime to ServerTime.toString():
let Parameters="timestamp="+ServerTime.toString()+"&"+"recvWindow="+MaxDelay;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | michelmm |
Solution 2 | Oscar Acevedo |
Solution 3 |