-1022 Signature for this request is not valid.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
</head>

<body>
    <script>
		let apikey = '';
		let secretKey = "";
		var serverTime = new Date().getTime() + 1000;
		var urlStr = "timestamp="+serverTime+"&recvWindow=1000";
		
		function sha256Sign(secretKey, data) {
			const dataBytes = CryptoJS.enc.Utf8.parse(data);

			const hash = CryptoJS.HmacSHA256(dataBytes, secretKey);

			const signature = hash.toString(CryptoJS.enc.Hex);

			return signature;
		}

		let data = JSON.stringify(urlStr);

		let signature = sha256Sign(secretKey, data);
		var url = 'https://fapi.binance.com/fapi/v3/account?'+urlStr+"&signature="+signature;
		console.log("===========v:"+url);
		
		
		$.ajax({
            url: url,
            type: 'GET',
            headers: {
                'X-MBX-APIKEY': apikey
            },
            success: function (response) {
                console.log('success:', response);
            },
            error: function (error) {
                console.log('fail:', error);
            }
        });
    </script>
</body>

</html>

刚开始是提示Timestamp for this request is outside of the recvWindow.通过增加1000ms解决了,但是现在一直提示-1022 Signature for this request is not valid.,检查了apiKey正常,headers也带了,参数顺序也检查了,没发现异常。

请各位大佬帮忙指导下

This is most likely the issue.

Sign urlStr directly:

let signature = sha256Sign(secretKey, urlStr);
1 Like

非常感谢,确实是这个问题。

再次感谢!