【问题标题】:API0005 Invalid Signature Connection Error from Google Apps Script to BitStamp APIAPI0005 从 Google Apps 脚本到 BitStamp API 的无效签名连接错误
【发布时间】:2019-10-23 18:54:05
【问题描述】:

我正在尝试将来自我的 BitStamp 帐户的不同信息显示到 Google 电子表格上。 为此,我们使用了 Google Apps Script (GAS),当然还有 Javascript。

我得到的错误是 API0005,as you can see from BitStamp API reference page,代表 invalid signature:

API0005    无效签名    发布的签名与我们的不匹配

现在,我一直在从多个来源收集信息,尤其是在 Stack 上,但无法确定问题出在哪里。

我注意到了一些事情,我想强调一下,因为我猜这个问题可能与此有关:

为了便于构建,我将签名合成过程的主要步骤附加到电子表格本身,以便更好地了解其开发。

你会注意到 nonce 输出的微小变化,即使在一行代码和下一行代码之间,每次调用或调用函数时;这并不让我感到惊讶,因为我猜每次调用 nonce 都经过了几毫秒,并且输出 必须 不同(毕竟我猜它是专门为此设计的)。

但我的第一个问题是:即使调用toUpperCase() 转换,它也可以改变吗? (顺便说一句,我猜这不应该是问题的原因)

var nonce = new (function() {
    this.generate = function() {
        var now = Date.now();
        this.counter = (now === this.last? this.counter + 1 : 0);
        this.last    = now;
        // add padding to nonce
        var padding = 
            this.counter < 10 ? '000' : 
                this.counter < 100 ? '00' :
                    this.counter < 1000 ?  '0' : '';
        return now+padding+this.counter;
    };
})();

//funzione write
function write() {

var cred = {
       id:'digit2118',
      key:'2RhZfUKIYJbT8CBYk6T27uRSF8Gufre',
   secret:'T8CBYk6T274yyR8Z2RhZfUxbRbmZZHJ'
};

//adding some cells output to monitor each step of the "conversion" 
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B14");
cell.setValue(cred.id);
// .. and so on, see screen cap..

var message = nonce.generate() + cred.id +  cred.key;

var res = Utilities.computeHmacSha256Signature(cred.secret, message).map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");
var signature = res.toUpperCase();

// qui mettiamo i dati per fare la comunicazione vera e propria
var data = {
  key: cred.key,
  signature: res,
  nonce: nonce.generate()
};

var options = {
  'method' : 'post',
  //'contentType': 'application/json',
  // Convert the JavaScript object to a JSON string.
  //'payload' : JSON.stringify(data)
  'muteHttpExceptions' : true,
  'payload' : data
};

var risposta = UrlFetchApp.fetch('https://www.bitstamp.net/api/v2/balance/', options);
  var risposta2 = JSON.parse(risposta.getContentText());

  Logger.log(risposta2);
  //Logger.log(risposta.getContentText()); 
return signature;
}//end of write();

Logger.log(write());

所以最后我看不到在哪里,但这一定是我缺少的东西。

(ps:这是我从中获得 Nonce 代码的地方:Generate Nonce,)

编辑:问题已解决

更新了以下问题和解决方案的代码。

感谢@Tanaike

【问题讨论】:

    标签: javascript api google-apps-script google-sheets nonce


    【解决方案1】:

    这个修改怎么样?

    修改点:

    • Utilities.computeHmacSha256Signature(value, key)的参数中,依次为valuekey
      • 请将Utilities.computeHmacSha256Signature(cred.secret, message)修改为Utilities.computeHmacSha256Signature(message, cred.secret)

    修改脚本:

    从:
    var res = Utilities.computeHmacSha256Signature(cred.secret, message).map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");
    
    到:
    var res = Utilities.computeHmacSha256Signature(message, cred.secret).map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");
    

    注意:

    • 如果上述修改后出现错误,请将nonce修复为请求中的常量值,然后重试。
      • 我认为nonce 可能需要在请求中进行修复。关于这一点,请在您的环境中进行测试。
      • 因为当我看到a sample script for Node.js 时,nonce 在请求中被固定为常量值。

    参考:

    我无法在我的环境中测试此修改。因此,如果这不是您问题的直接解决方案,我深表歉意。如果此修改更改了错误消息,请同时提供。

    【讨论】:

    • 感谢@Tanaike,我会尽快尝试您的建议并回复您!
    • 所以,我尝试修复我的错误,如您明智地建议的那样切换“值”和“键”,但发生了同样的错误。关于将nonce 变成常量,我该怎么做?你的意思是?如果可以的话请给我一些代码谢谢
    • 天啊!!好像现在可以了!我想这是一个非常愚蠢的疏忽。我现在要编辑我的主要问题并附加结果
    • @John Galassi 感谢您的回复。很高兴您的问题得到解决。
    【解决方案2】:

    结合@Tanaike 提供的明智建议,即在Utilities.computeHmacSha256Signature(value, key) 命令中正确切换valuekey,事情还没有奏效,我仍然习惯于得到一个无效的签名API0005 错误。

    长话短说,问题在于代码中的另一个疏忽:

    我正确地将签名切换为toUpperCase(),但是在将数组发送到BitStamp时,我使用的是小写版本,即res而不是signature

    var signature = res.toUpperCase();
    
    // qui mettiamo i dati per fare la comunicazione vera e propria
    var data = {
      key: cred.key,
      signature: res,
      nonce: nonce.generate()
    };
    

    细节已修复,现在可以正常工作了! 这是完整且更新的工作代码,供您参考:

    //funzione write
    function write() {
    
    /* nuova funzione nonce */
    _generateNonce = function() {
      var now = new Date().getTime();
    
      if(now !== this.last)
        this.nonceIncr = -1;
    
      this.last = now;
      this.nonceIncr++;
    
      // add padding to nonce incr
      // @link https://stackoverflow.com/questions/6823592/numbers-in-the-form-of-001
      var padding =
        this.nonceIncr < 10 ? '000' :
          this.nonceIncr < 100 ? '00' :
            this.nonceIncr < 1000 ?  '0' : '';
      return now + padding + this.nonceIncr;
    }
    
     var nonce = this._generateNonce(); //fine funzione
    
    var cred = {
           id:'digit2118',
          key:'2RhZfUKIYJbT8CBYk6T27uRSF8Gufrer',
       secret:'T8CBYk6T274yyR8Z2RhZfUxbRbmZZHJr'
    };
    
    var message = nonce + cred.id +  cred.key;
    
    var res = Utilities.computeHmacSha256Signature(message, cred.secret).map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");
    
    var signature = res.toUpperCase();
    
    var data = {
      key: cred.key,
      signature: signature,
      nonce: nonce
    };
    
    var options = {
      'method' : 'post',
      //'contentType': 'application/json',
      // Convert the JavaScript object to a JSON string.
      //'payload' : JSON.stringify(data)
      'muteHttpExceptions' : true,
      'payload' : data
    };
    
    var risposta = UrlFetchApp.fetch('https://www.bitstamp.net/api/v2/balance/', options);
      var risposta2 = JSON.parse(risposta.getContentText());
      var risposta3 = risposta2['usd_balance'];
      Logger.log(risposta3);
    return signature;
    }//end of write();
    
    

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 2022-10-16
      • 2021-06-02
      • 1970-01-01
      • 2014-08-08
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多