Binance API fetch在Chrome中的问题

rbpvctlc  于 2023-07-31  发布在  Go
关注(0)|答案(3)|浏览(110)

我的代码在anWriter应用程序中工作正常,但在Chrome浏览器中不工作!
到达 Account_Fetch 函数中的 * Fetch * 行后冻结!
如果代码 * 工作正常 * 它将显示 *OK * 消息!

文档

  • 端点安全类型 *
  • 签名端点安全 *
  • 定时安全 *
  • 账户信息 *
  • 备注 *:该账号用于 * 测试 *,所以我分享了我的 API KeySecret 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>

字符串

iklwldmw

iklwldmw1#

看看下面的代码-这一个运行到最后。然而,我被告知,“你不能从浏览器发出签名的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>

字符串

4dc9hkyq

4dc9hkyq2#

一切看起来都很好,除了在HmacSHA256方法的末尾缺少一个toString()

let Signature = CryptoJS.HmacSHA256(Parameters,SecretKey).toString();

字符串
这个方法返回一个像这样的对象(例如):

{words: [8], sigBytes: 32}
words: [8]
0: 670167524
1: 2092892393
2: 560578323
3: 567480325
4: -1211153183
5: -1038754858
6: 926457206
7: 683147716
sigBytes: 32


因此,您需要将其转换为字符串。

eoxn13cs

eoxn13cs3#

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

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

字符串

相关问题