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

问题描述 投票:0回答:1

使用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")

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

非常感谢。

api authentication google-sheets
1个回答
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 for the endpoint of 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]];,你可以使用以下脚本。

Script :

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

Note :

  • the document看,似乎WALLET_TYPECURRENCYBALANCE的指数分别总是012
© www.soinside.com 2019 - 2024. All rights reserved.