javascript - promise GET 调用休息 api : can't get a valid signature

标签 javascript node.js encryption es6-promise

我真的很难理解 js 的行为。

var rp = require('request-promise');
var crypto = require('crypto');



var options = {

  method : 'GET',
  uri : "https://api.binance.com/api/v1/order",

  qs: {
    signature : hash,

    timestamp : Date.now(),
    symbol : 'LTCBTC' 
  },
 headers: {
    'X-MBX-APIKEY' : 'PUBLICKEY'
  },
    json : true
};

var hash= crypto.createHmac('sha256', options.toString())
.update('SECRETKEY')
.digest('hex');

 //console.log(hash);
rp(options)

    .then(function (Bbody) {
        console.log(Bbody);
    })

    .catch(function (err) {
      console.log(err);
    });

如果我将hash 函数放在options 之前

它说(显然)

TypeError: Cannot read property 'toString' of undefined

但是如果我把它放在我分享的代码中,我会得到这个错误:

StatusCodeError: 400 - {"code":-1102,"msg":"Mandatory parameter 'signature' was not sent, was empty/null, or malformed."}

这是请求的输出:

 options:                                                                                                                 
{ method: 'GET',                                                                                                          
uri: 'https://api.binance.com/api/v1/order',                                                                            
qs:                                                                                                                      
{ signature: undefined,                                                                                                   
timestamp: 1540392736646,                                                                                               
symbol: 'LTCBTC' },                                                                                                  
headers:                                                                                                                 
{ 'X-MBX-APIKEY': 
'PUBLICKEY' },                                
json: true,                                                                                                             
callback: [Function: RP$callback],                                                                                      
transform: undefined,                                                                                                   
simple: true,                                                                                                           
resolveWithFullResponse: false,                                                                                         
transform2xxOnly: false }, 

如果我取消对 console.log() 的注释,我会在请求拒绝之前将正确的散列打印在视频上作为第一个输出。还是电话接不上。

我用过的文档:

binance api documentation

this node documentation for the crypto library

and this npm doc to promisify api calls

附言: PUBLICKEYSECRETKEY 是占位符,但在我的测试中我使用了正确的字符串。

最佳答案

有一种东西叫variable hoisting在 JavaScript 中。基本上,JavaScript 会偷偷移动你的 hash 的声明。脚本开头的变量为 var hash; .当您构建 options变量,hash仍未定义。

除了变量提升之外,我认为您无法生成应包含相同散列值的 JavaScript 对象的散列。根据https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#signed-endpoint-examples-for-post-apiv1order ,您需要执行以下步骤:

  1. 为您的请求获取所有输入参数(符号、面、...、时间戳),并将它们组合成一个查询字符串,例如symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559
  2. 根据查询字符串计算 HMAC SHA256签名
  3. 通过以下两种方式之一提出请求:
    • 将您所有的输入参数签名作为查询参数包含在 URL 中(例如:https://api.binance.com/api/v3/order?symbol=LTCBTC&side=BUY&type=LIMIT&...&signature=<your sha256 here>)
    • 在请求正文中包含所有输入参数签名,再次使用& 连接起来

关于javascript - promise GET 调用休息 api : can't get a valid signature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52972380/

相关文章:

javascript - 覆盖 Javascript 中的全局函数声明

javascript - 使用 WebStorm 文件夹中的闭包缩小所有 js 文件

java - 如何在使用 Glide 加载之前解密图像

azure - CosmosDB 静态加密

javascript - 加载新内容时调整 iframe 的高度

javascript - 显示模块模式没有设置我的私有(private)变量,为什么?

javascript - 如何确保 JSONModel 的数据已加载?

javascript - 如何检查用户是否已经登录? (everyauth,node.js)

Node.js、Socket.io 多用户应用程序——存储用户 session ?

ruby-on-rails - rails 加密/解密