Michael Hazilias
Michael Hazilias

Reputation: 19

Connecting to Bitfinex API from Google Sheets

I'm trying to connect my google sheet to Bitfinex through the 'authenticated' section of the API so I can access my account information. Here is the API link.

I haven't been able to get the 'request' or 'crypto' libraries to work so I've been trying to use other available functions in google sheets, but am having trouble.

Following is the code snippet I'm using:

  var completeURL = "https://api.bitfinex.com/v1/account_infos";
  var nonce = Math.floor(new Date().getTime()/1000);

  var body = {
    'request' : completeURL,
    'nonce' : nonce
  };

  var payload = JSON.stringify(body).toString('base64');
  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_384,
                                                 payload,
                                                 secret);

  signature = signature.map(function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('');

  var params = {
    headers: {
      'X-BFX-APIKEY': key,
      'X-BFX-PAYLOAD': payload,
      'X-BFX-SIGNATURE': signature
    },
  }

  Logger.log(completeURL);
  Logger.log(params);

  var response = UrlFetchApp.fetch(completeURL, params);
  var json = JSON.parse(response.getContentText());

I get the following error from the API:

Request failed for https://api.bitfinex.com/v1/account_infos returned code 400. Truncated server response: {"message":"Invalid json."} (use muteHttpExceptions option to examine full response). (line 209, file "Code")

And the following are the values from the Logger.log calls:

[17-09-24 16:22:28:170 AEST] https://api.bitfinex.com/v1/account_infos
[17-09-24 16:22:28:171 AEST] {headers={X-BFX-PAYLOAD={"request":"https://api.bitfinex.com/v1/account_infos","nonce":1506234148}, X-BFX-SIGNATURE=06d88a85098aefbf2b56af53721506863978f9350b1b18386c23f446254789dbbfc1eeb520bdfc7761b30f98ea0c21a2, X-BFX-APIKEY=ak6UoPiwaLjwt2UqDzZzZGjpb9P2opvdPCAIqLy0eVq}}

I'm stuck and not sure what else to try?

Can anyone spot what I'm doing wrong?

Upvotes: 1

Views: 961

Answers (3)

Dmitry Bondarenko
Dmitry Bondarenko

Reputation: 1

That's how it works

  var body = {
    'request' : "/v1/balances",
    'nonce' : nonce,
    'options':{}
  };

Upvotes: 0

Tanaike
Tanaike

Reputation: 201378

How about this modification? Since I have no secret, I couldn't debug this sample. So I don't know whether this modified sample works. I'm sorry.

Modification points :

  1. secret is not defined.
  2. When POST method is used, it requires to include method: "post" to UrlFetchApp.fetch().
  3. When it reads Javascript sample of the document, signature has to be modified.
  4. When it reads Javascript sample of the document, body: JSON.stringify(body) is included in the request parameters.
  5. There is an error message of {"message":"Invalid json."}.

The script which was reflected above modifications is as follows.

Modified script :

var secret = "#####"; // Please input this.

var completeURL = "https://api.bitfinex.com/v1/account_infos";
var nonce = Math.floor(new Date().getTime()/1000);
var body = {
  'request' : completeURL, // I don't know whether this is the correct value.
  'nonce' : nonce
};

var payload = Utilities.base64Encode(Utilities.newBlob(JSON.stringify(body)).getDataAsString());
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_384, payload, secret);
signature = signature.map(function(byte) {
  return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');

var params = {
  method: "post",
  headers: {
    'X-BFX-APIKEY': key,
    'X-BFX-PAYLOAD': payload,
    'X-BFX-SIGNATURE': signature
  },
  payload: JSON.stringify(body),
  contentType: "application/json",
  muteHttpExceptions: true
}
var response = UrlFetchApp.fetch(completeURL, params);
var json = JSON.parse(response.getContentText());

If this was not useful for you, I'm sorry.

Upvotes: 1

Binarus
Binarus

Reputation: 4405

I am not sure if I am understanding your code, but if I do, there is at least one oddity at first sight:

In computeHmacSignature(...), you are using the variable secret which has not been initialized or even declared anywhere.

Upvotes: 0

Related Questions