使用Google Apps脚本签署对Coinbase Pro API的调用 [英] Sign a call to Coinbase Pro API with Google Apps Script

查看:130
本文介绍了使用Google Apps脚本签署对Coinbase Pro API的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很生气,试图使用Google Apps脚本将我的第一个API调用发送到Coinbase Pro.在node.js中非常简单( https://docs.pro.coinbase.com/#signing-a-message ),但对Google脚本执行相同操作只是一次又一次地返回无效签名".

I'm going mad trying to send my first API call to Coinbase Pro using Google Apps Script. In node.js is pretty easy (https://docs.pro.coinbase.com/#signing-a-message) but doing the same with Google scripts is just returning again and again "Invalid Signature".

这是我正在使用的代码:

This is the code I'm using:

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);    

}

我真的需要帮助:-)-谢谢!

I really need help :-) - Thanks!

推荐答案

在您的脚本中,它假定您的请求标头(CB-ACCESS-SIGN的值和终结点除外)是正确的.请注意这一点.

In your script, it supposes that your request headers except for the value of CB-ACCESS-SIGN and endpoint are correct. Please be careful this.

  • 对于Utilities.base64Decode(secret).toString(),该数组将转换为字符串.我认为这可能是您遇到问题的原因.
  • In the case of Utilities.base64Decode(secret).toString(), the array is converted to the string. I think that this might be the reason of your issue.

当上述点被反映时,它变成如下.

When above point is reflected, it becomes as follows.

在这种情况下,功能SignAPICall被修改.

In this case, the function SignAPICall is modified.

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是字节数组.
    • In this case, value and key of computeHmacSha256Signature(value, key) are the byte array.
      • 当我通过比较官方文档,我可以确认可以获得相同的结果.
      • 不幸的是,我无法使用上述修改后的脚本测试对API的请求,同时我可以确认从上述修改后的脚本中检索到官方文档中示例脚本的相同签名.因此,请在您的环境中测试请求.当您使用上述修改后的脚本向API请求时,如果发生错误,请再次检查请求标头,端点和密码.
      • When I checked above modified script by comparing the sample scripts of the official document, I could confirm that the same result can be obtained.
      • Unfortunately, I cannot test the request to the API using above modified script while I can confirm that the same signature from the sample script at the official document is retrieved from the above modified script. So please test the request in you environment. When you requested to the API using above modified script, when an error occurs, please check the request headers, endpoint and secret, again.
      • Signing a Message
      • base64Decode(encoded)
      • computeHmacSha256Signature(value, key)

      这篇关于使用Google Apps脚本签署对Coinbase Pro API的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆