【问题标题】:Sign a call to Coinbase Pro API with Google Apps Script使用 Google Apps 脚本签署对 Coinbase Pro API 的调用
【发布时间】: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


【解决方案1】:

在您的脚本中,它假定您的请求标头(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;
}
  • 在这种情况下,computeHmacSha256Signature(value, key) 中的valuekey 是字节数组。

注意:

  • 当我通过比较the official document的示例脚本检查上述修改后的脚本时,我可以确认可以获得相同的结果。
  • 很遗憾,我无法使用上述修改后的脚本测试对 API 的请求,但我可以确认从上述修改后的脚本中检索到来自官方文档示例脚本的相同签名。因此,请在您的环境中测试请求。当您使用上述修改后的脚本向 API 请求时,如果发生错误,请再次检查请求标头、端点和密钥。

参考资料:

【讨论】:

    【解决方案2】:

    我终于找到了解决办法。我提交给“Utilities.computeHmacSha256Signature”的数据类型有问题。在代码中你可以发现函数运行良好。

    function SignAndCallAPI(method, requestPath, 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);  
    
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 2020-09-05
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多