使用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中的keycomputeHmacSha256Signature(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);  

}