如何解决使用Google Apps脚本签署对Coinbase Pro API的调用 修改点:修改后的脚本:注意:参考文献:
我很生气,试图使用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,method,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);
}
我真的需要帮助:-)-谢谢!
解决方法
在您的脚本中,它假设您的请求标头(CB-ACCESS-SIGN
和端点的值除外)是正确的。请注意这一点。
修改点:
- 对于
Utilities.base64Decode(secret).toString()
,该数组将转换为字符串。我认为这可能是您遇到问题的原因。
当上述点被反映时,它如下。
修改后的脚本:
在这种情况下,功能SignAPICall
被修改。
function SignAPICall(secret,timestamp,method,requestPath,body) {
var what = (timestamp + method + requestPath + body);
var decodedsecret = Utilities.base64Decode(secret); // Modified
var res = Utilities.computeHmacSha256Signature(Utilities.newBlob(what).getBytes(),decodedsecret); // Modified
hmac = Utilities.base64Encode(res);
return hmac;
}
- 在这种情况下,
value
中的key
和computeHmacSha256Signature(value,key)
是字节数组。
注意:
- 当我通过比较the official document的示例脚本检查以上修改后的脚本时,我可以确认可以获得相同的结果。
- 不幸的是,我无法使用上述修改后的脚本测试对API的请求,同时我可以确认从上述修改后的脚本中检索到官方文档中示例脚本的相同签名。因此,请在您的环境中测试请求。当您使用上述修改后的脚本向API请求时,如果发生错误,请再次检查请求标头,端点和密码。
参考文献:
,我终于找到了解决方案。我提交给“ Utilities.computeHmacSha256Signature”的数据类型存在问题。在代码中,您可以找到功能正常的
。
function SignAndCallAPI(method,body) {
var timestamp = Math.floor(Date.now() / 1000).toString();
var what = Utilities.base64Decode(Utilities.base64Encode(timestamp + method + requestPath + body));
var decodedsecret = Utilities.base64Decode(globalvars_CB.secret);
var hmac = Utilities.base64Encode(Utilities.computeHmacSha256Signature(what,decodedsecret));
var options = {
'method' : method,'muteHttpExceptions' : true,'headers' : {
'Content-Type': 'application/json','CB-ACCESS-KEY' : globalvars_CB.apikey,'CB-ACCESS-SIGN' : hmac,'CB-ACCESS-TIMESTAMP' : timestamp,'CB-ACCESS-PASSPHRASE' : globalvars_CB.passphrase,}
}
var responseJson = UrlFetchApp.fetch(globalvars_CB.uri+requestPath,options);
Logger.log(responseJson);
return(responseJson);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。