猿问

API0005 从 Google Apps 脚本到 BitStamp API 的无效签名连接错误

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

我得到的错误是 API0005,正如您从 BitStamp API 参考页面中看到的,它代表无效签名:

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

};


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


侃侃无极
浏览 176回答 2
2回答

三国纷争

这个改装怎么样?改装要点:在的参数Utilities.computeHmacSha256Signature(value, key),它的value和key秩序。请修改Utilities.computeHmacSha256Signature(cred.secret, message)为Utilities.computeHmacSha256Signature(message, cred.secret).修改后的脚本:从:var&nbsp;res&nbsp;=&nbsp;Utilities.computeHmacSha256Signature(cred.secret,&nbsp;message).map(function(e)&nbsp;{return&nbsp;("0"&nbsp;+&nbsp;(e&nbsp;<&nbsp;0&nbsp;?&nbsp;e&nbsp;+&nbsp;256&nbsp;:&nbsp;e).toString(16)).slice(-2)}).join("");至:var&nbsp;res&nbsp;=&nbsp;Utilities.computeHmacSha256Signature(message,&nbsp;cred.secret).map(function(e)&nbsp;{return&nbsp;("0"&nbsp;+&nbsp;(e&nbsp;<&nbsp;0&nbsp;?&nbsp;e&nbsp;+&nbsp;256&nbsp;:&nbsp;e).toString(16)).slice(-2)}).join("");笔记:如果上述修改后出现错误,请nonce在请求中修复为常量值,然后重试。我认为这nonce可能需要在请求中修复。关于这一点,请在您的环境中进行测试。因为当我看到Node.js 的示例脚本时,nonce它在请求中被固定为一个常量值。参考:computeHmacSha256Signature(value, key)我无法在我的环境中测试此修改。因此,如果这不是您问题的直接解决方案,我深表歉意。如果此修改更改了错误消息,也请提供。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答