简体   繁体   English

Chrome 中的 Binance API Fetch 问题

[英]Issue With Binance API Fetch In Chrome

My Code Works Fine InanWriter App But Doesn't Work In Chrome Browser !我的代码在anWriter应用程序中运行良好,但在Chrome浏览器中无法运行!

It Freezes After Reaching The Fetch Line In Account_Fetch Function !到达Account_Fetch函数中的提取行后冻结!

If Code Works Fine It Will Show OK Message !如果代码运行良好,它将显示OK消息!

Documentation :文档

Endpoint Security Type 端点安全类型

SIGNED Endpoint Security 签名端点安全

Timing Security 计时安全

Account Information 帐户信息

Note : This Account Is For Test And That's Why I Share My API Key And Secret Key !注意:此帐户用于测试,这就是我分享我的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>

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"但是我被告知“你不能从浏览器发出签名的 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>

Everything looks good except you're missing a toString() at the end of HmacSHA256 method.除了在 HmacSHA256 方法的末尾缺少一个toString()之外,一切看起来都很好。

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.因此,您需要将其转换为字符串。

并需要将 ServerTime 更改为 ServerTime.toString():

let Parameters="timestamp="+ServerTime.toString()+"&"+"recvWindow="+MaxDelay;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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