【问题标题】:promisified GET call to rest api: can't get a valid signature对 REST API 的承诺 GET 调用:无法获得有效签名
【发布时间】:2018-10-24 15:08:26
【问题描述】:

我真的很难理解 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: 无法读取未定义的属性“toString”

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

StatusCodeError: 400 - {"code":-1102,"msg":"强制参数 'signature' 未发送,为空/null,或格式错误。"}

这是请求的输出:

 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 是占位符,但在我的测试中我使用了正确的字符串。

【问题讨论】:

  • 你缺少一个键,函数调用是crypto.createHmac(algorithm, key[, options]) 你还需要一个键,options 是可选参数并且执行options.toString() 不会返回一个字符串值。您现在想通过简单地添加一个附加参数来更正您的代码var hash= crypto.createHmac('sha256', 'secretkeytoken', options)

标签: javascript node.js encryption es6-promise


【解决方案1】:

在 JavaScript 中有一个叫做 variable hoisting 的东西。基本上,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>
    • 在请求正文中包含所有输入参数签名,再次使用&连接

【讨论】:

  • 根据github.com/binance-exchange/binance-official-api-docs/blob/…,您需要根据symboltimestamp 等查询参数计算散列值,之后您需要包含另一个名为@987654333 的查询参数@ 你提供哈希的地方。
  • 您能否编辑您的问题以详细说明此评论? Bc 当我读到它时,我是这样理解的: > 使用您的 secretKey 作为键,totalParams 作为 HMAC 操作的值 >totalParams 被定义为与请求正文连接的查询字符串。对我来说,totalParams 是整个选项功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
  • 2022-11-28
  • 1970-01-01
  • 2013-01-16
相关资源
最近更新 更多