API0005 从 Google Apps 脚本到 BitStamp API 的无效签名连接错误
API0005 Invalid Signature Connection Error from Google Apps Script to BitStamp API
我正在尝试将我的 BitStamp 帐户中的不同信息显示到 Google 电子表格中。
为了做到这一点,使用了 Google Apps 脚本 (GAS),当然还有 Javascript。
我得到的错误是 API0005,as you can see from BitStamp API reference page,代表 无效签名:
API0005 Invalid signature Posted signature doesn't match with ours
现在,我一直在从多个来源收集信息,尤其是在 Stack 上,但不太清楚问题出在哪里。
虽然我注意到了一些事情,但我想强调一下,因为我想问题可能与此有关:
为了便于构建,我将签名合成过程的主要步骤ps附加到电子表格本身,以便更好地了解其发展.
您会注意到 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
这个修改怎么样?
修改点:
- 在
Utilities.computeHmacSha256Signature(value, key)
的参数处,依次是value
和key
。
- 请将
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 提供的明智建议,即在 Utilities.computeHmacSha256Signature(value, key)
命令中正确切换 value
和 key
后,事情还没有奏效,我仍然习惯了收到无效签名 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
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();
我正在尝试将我的 BitStamp 帐户中的不同信息显示到 Google 电子表格中。 为了做到这一点,使用了 Google Apps 脚本 (GAS),当然还有 Javascript。
我得到的错误是 API0005,as you can see from BitStamp API reference page,代表 无效签名:
API0005 Invalid signature Posted signature doesn't match with ours
现在,我一直在从多个来源收集信息,尤其是在 Stack 上,但不太清楚问题出在哪里。
虽然我注意到了一些事情,但我想强调一下,因为我想问题可能与此有关:
为了便于构建,我将签名合成过程的主要步骤ps附加到电子表格本身,以便更好地了解其发展.
您会注意到 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
这个修改怎么样?
修改点:
- 在
Utilities.computeHmacSha256Signature(value, key)
的参数处,依次是value
和key
。- 请将
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 提供的明智建议,即在 Utilities.computeHmacSha256Signature(value, key)
命令中正确切换 value
和 key
后,事情还没有奏效,我仍然习惯了收到无效签名 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
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();