【发布时间】:2020-09-06 14:56:59
【问题描述】:
尝试使用 Google Apps 脚本将我的第一个 API 调用发送到 Coinbase Pro,我快疯了。在 node.js 中非常简单 (https://docs.pro.coinbase.com/#signing-a-message),但对 Google 脚本执行相同操作只会一次又一次地返回“无效签名”。
这是我正在使用的代码:
function GetMyAccounts () {
var globalvars_CB = {
'apikey' : 'f7d20a*******18c',
'secret' : '******pIIitRbWCv9N/mMWaR*****mGQMuI+m/vSbU1zuh5U6WFiFw==',
'passphrase' : 'ceacdsewfcsa',
'uri' : 'https://api.pro.coinbase.com'
}
var requestPath = '/accounts';
var timestamp = Math.floor(Date.now() / 1000);
var options = {
'method' : 'GET',
'muteHttpExceptions' : true,
'headers' : {
'Content-Type': 'application/json',
'CB-ACCESS-KEY' : globalvars_CB.apikey,
'CB-ACCESS-SIGN' : SignAPICall(globalvars_CB.secret, timestamp, 'GET', requestPath, ''),
'CB-ACCESS-TIMESTAMP' : timestamp,
'CB-ACCESS-PASSPHRASE' : globalvars_CB.passphrase,
}
}
var responseJson = UrlFetchApp.fetch(globalvars_CB.uri+requestPath, options);
Logger.log(responseJson);
}
function SignAPICall(secret, timestamp, method, requestPath, body) {
var what = (timestamp + method + requestPath + body);
var decodedsecret = Utilities.base64Decode(secret).toString();
var hmac = Utilities.computeHmacSha256Signature(what, decodedsecret);
hmac = Utilities.base64Encode(hmac);
return (hmac);
}
我真的需要帮助 :-) - 谢谢!
【问题讨论】:
-
我提出了一个修改后的脚本作为答案。你能确认一下吗?不幸的是,我无法使用上述修改后的脚本测试对 API 的请求。那么你可以在你的环境中测试请求吗?如果这不是直接的解决方案,我深表歉意。
-
我的回答是否向您展示了您想要的结果?你能告诉我吗?这对我学习也很有用。如果这可行,与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果您对我的回答有疑问,我深表歉意。到时候,我能问一下你现在的情况吗?我想学习解决你的问题。
-
您好,感谢您的回复。我以为我已经回答了...您的更改无效,原因是“Utilities.computeHmacSha256Signature”函数需要两个字符串或两个字节[] nut而不是它的组合。我将发布正确的代码,使其运行良好。谢谢
-
感谢您的回复。我的回答建议
computeHmacSha256Signature(byte, byte),因为您使用的是var decodedsecret = Utilities.base64Decode(secret)。所以我使用了Utilities.newBlob(what).getBytes()。在您的附加答案中,我认为Utilities.base64Decode(Utilities.base64Encode(timestamp + method + requestPath + body))与Utilities.newBlob(what).getBytes()相同。所以我无法理解我的答案和你的答案之间的区别。对此我深表歉意。
标签: google-apps-script base64 sha256 coinbase-api