JS KuCoin API - KC-API-SIGN 无效 - 如何签名

问题描述 投票:0回答:1

我正在尝试使用以下代码查看我的帐户的未结订单:

import { KEY, PASSWORD, SECRET } from "./secrets.js";
import CryptoJS from "crypto-js";

const baseUrl = 'https://api.kucoin.com'
const endPointOrders = '/api/v1/orders';

const signatureInBase64 = (path, now) => {
  let msg = now + "GET" + path
  let signature = CryptoJS.HmacSHA256(msg, SECRET);
  return CryptoJS.enc.Base64.stringify(signature);
}

const passphraseInBase64 = () => {
  var passphrase = CryptoJS.HmacSHA256(PASSWORD, SECRET);
  return CryptoJS.enc.Base64.stringify(passphrase);
}

const openOrders = async() => {
  let path = endPointOrders;
  let now = Date.now();
  var headers = {
    "KC-API-SIGN": signatureInBase64(path, now),
    "KC-API-TIMESTAMP": now,
    "KC-API-KEY": KEY,
    "KC-API-PASSPHRASE": passphraseInBase64(),
    "KC-API-KEY-VERSION": "2",
    "Content-Type": "application/json"
  }
  const url = baseUrl + path;

  const response = await fetch(url, {
    method: 'post',
    body: "",
    headers: headers
  });
  const data = await response.json();

  console.log(data);

}

export { openOrders };

我得到的回应是

{ code: '400005', msg: 'Invalid KC-API-SIGN' }

我做错了什么?我尝试过搜索这个,但当前所有问题都是针对python版本的,这是使用nodeJS

javascript node.js cryptojs cryptocurrency kucoin
1个回答
0
投票

差不多就对了。如果 api 调用是 POST 请求,则签名中的方法应为“POST”:

const signatureInBase64 = (path, now) => {
  let msg = now + "POST" + path
  let signature = CryptoJS.HmacSHA256(msg, SECRET);
  return CryptoJS.enc.Base64.stringify(signature);
}
© www.soinside.com 2019 - 2024. All rights reserved.