0

在我的应用程序中,我使用了使用APIasync/await的逻辑。

问题是我无法弄清楚为什么该check()功能可以毫不费力地工作:

const KEY    = 'my key';
const SECRET = 'my secret';

function getSign(form) {
  return crypto.createHmac('sha512', SECRET).update(form).digest('hex').toString();
}

async function check() {
  let form    = {};
  let header  = {'Content-Type':'application/x-www-form-urlencoded'};
  header.KEY  = KEY;
  header.SIGN = await getSign(querystring.stringify(form));

  // Checking the state (works)!
  let info = await fetch(API_TRADE_URL + OPENORDERS_URL, { method:'POST', headers:header, form:form })
      info = info.json();

  return info;
}

… 当placeOrder()函数失败并且 API 发送消息时,KEY/SECRET ( Invalid data) 存在问题:

async function placeOrder ( currencyPair, rate, amount ) {
  let form     = { 'currencyPair':currencyPair, 'rate':rate, 'amount':amount };
  let header   = {};
  header.KEY   = KEY;
  header.SIGN  = getSign(querystring.stringify(form));

  // Here I am placing the order (does not work!)
  let orderRes = await fetch(API_TRADE_URL + BUY_URL, { method:'POST', headers:header, form:form } );
      orderRes = orderRes.json();
  return orderRes;
}

我对 KEY/SECRET 对或函数持怀疑态度,getSign()但在 API 站点上作为演示提供的buy()函数(基于 Node's request)(我试图模仿的函数)按预期工作:

function buy(currencyPair, rate, amount, cp) {
   let form = {'currencyPair':currencyPair,'rate':rate,'amount':amount};
   let header = {};
   header.KEY = KEY;
   header.SIGN = getSign(querystring.stringify(form));

   // Here I am placing the order (works!)
   Request({method: 'POST', url: API_TRADE_URL + BUY_URL, headers: header, form:form }, function (res) { console.log(res) });
 }

这里重要的是我不想使用该buy()函数,因为我需要在没有回调的情况下工作。我使用相同的数据,相同的逻辑(在check()上面提到的函数中工作),但在函数的情况下它失败了placeOrder

4

0 回答 0