在 Chrome 中使用 Binance API 获取问题

时间:2021-07-15 10:48:43

标签: javascript api google-chrome fetch binance

我的代码在 anWriter 应用中运行良好,但在 Chrome 浏览器中不起作用!

它在到达 Fetch 行后冻结 Account_Fetch 函数!

如果代码工作正常它会显示OK消息!

文档

Endpoint Security Type

SIGNED Endpoint Security

Timing Security

Account Information

注意:此帐户用于测试,这就是我分享我的的原因API 密钥秘密密钥

<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>

1 个答案:

答案 0 :(得分:0)

看看下面的代码 - 这一段一直运行到最后。但是我被告知“您不能从浏览器发出签名的 API 请求”

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>