如何使用Google表格从Bitfinex经过身份验证的api端点获取数据?

时间:2017-12-30 18:17:25

标签: api authentication google-sheets

使用Google表格,我已将我的api_key和api_secret分别存储在用户信息的属性服务部分" api_key"和api_secret"。

我想从我的帐户中获取钱包信息。我写的代码如下:

function wallet() {
  var api_key = PropertiesService.getScriptProperties().getProperty('api_key');
  var api_secret = PropertiesService.getScriptProperties().getProperty('api_secret');
  var response = UrlFetchApp.fetch("https://api.bitfinex.com/v2/auth/r/wallets", api_key, api_secret);
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet");
  var result = JSON.parse(response.getContentText());
  var wallet_btc = result.BALANCE
}

当我在调试模式下运行时,错误消息是:

Cannot find method fetch(string,null,null). (line 13, file "Code")

这种做法是错误的,代码错了,还是两者兼而有之?

非常感谢。

1 个答案:

答案 0 :(得分:1)

以下修改如何?

修改要点:

  • UrlFetchApp.fetch()的参数为UrlFetchApp.fetch(url, params)params是一个对象。
    • 这是错误Cannot find method fetch(string,null,null). (line 13, file "Code")
    • 的原因
  • 当我看到the sample scripts for Bitfinex API时,必须使用api_keyapi_secretnoncebodysignature创建请求正文。并且signature由HMAC_SHA_384加密并转换为无符号十六进制的字符串。
  • https://api.bitfinex.com/v2/auth/r/wallets端点的样本如下。这来自API reference

Sample表示https://api.bitfinex.com/v2/auth/r/wallets

的端点
request.post(
  `${url}/auth/r/wallets`,
  headers: { /* auth headers */ },
  body: {},
  json: true,
  (error, response, body) => console.log(body)
)

当以上几点反映到您的脚本时,修改后的脚本如下所示。

修改后的脚本:

function wallet() {
  var api_key = PropertiesService.getScriptProperties().getProperty('api_key');
  var api_secret = PropertiesService.getScriptProperties().getProperty('api_secret');

  var apiPath = "v2/auth/r/wallets";
  var nonce = Date.now().toString();
  var body = {};
  var rawBody = JSON.stringify(body);
  var signature = "/api/" + apiPath + nonce + rawBody;
  signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_384, signature, api_secret)
    .map(function(e) {
      var v = (e < 0 ? e + 256 : e).toString(16);
      return v.length == 1 ? "0" + v : v;
    }).join("");
  var url = "https://api.bitfinex.com/" + apiPath;
  var options = {
    method: 'POST',
    contentType: "application/json",
    headers: {
      'bfx-nonce': nonce,
      'bfx-apikey': api_key,
      'bfx-signature': signature
    },
    payload: rawBody
  };
  var response = UrlFetchApp.fetch(url, options);
  var result = JSON.parse(response.getContentText());
  Logger.log(result)
//  var wallet_btc = result.BALANCE // I couldn't confirm whether this key exists.
}

参考文献:

我无法确认这是否有效。如果这不起作用,你能告诉我情况吗?我想修改。

编辑:

如果您希望0.0957596的结果为[["exchange", "USD", 14.81076629, 0, null], ["exchange", "BTC", 0.0957596, 0, null], ["funding", "BTC", 4.13E-6, 0, null], ["funding", "ETH", 3.50186961, 0, null], ["exchange", "OMG", 5.9E-7, 0, null]];,则可以使用以下脚本。

脚本:

function wallet() {
  var api_key = PropertiesService.getScriptProperties().getProperty('api_key');
  var api_secret = PropertiesService.getScriptProperties().getProperty('api_secret');

  var apiPath = "v2/auth/r/wallets";
  var nonce = Date.now().toString();
  var body = {};
  var rawBody = JSON.stringify(body);
  var signature = "/api/" + apiPath + nonce + rawBody;
  signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_384, signature, api_secret)
    .map(function(e) {
      var v = (e < 0 ? e + 256 : e).toString(16);
      return v.length == 1 ? "0" + v : v;
    }).join("");
  var url = "https://api.bitfinex.com/" + apiPath;
  var options = {
    method: 'POST',
    contentType: "application/json",
    headers: {
      'bfx-nonce': nonce,
      'bfx-apikey': api_key,
      'bfx-signature': signature
    },
    payload: rawBody
  };
  var response = UrlFetchApp.fetch(url, options);
  var result = JSON.parse(response.getContentText());
//  Logger.log(result)
//  var wallet_btc = result.BALANCE // I couldn't confirm whether this key exists.

  var balance = 0;
  for (var i in result) {
    if (result[i][0] == "exchange" && result[i][1] == "BTC") {
      balance = result[i][2];
      break;
    }
  }
  Logger.log(balance)
}

注意:

  • the document开始,似乎WALLET_TYPECURRENCYBALANCE的索引始终为01和{{1分别在响应中的每个元素。