Home > Back-end >  FTX API in Google Sheets
FTX API in Google Sheets

Time:10-28

i keep getting this error "Exception: Request failed for https://ftx.com returned code 401. Truncated server response: {"success":false,"error":"Not logged in"} (use muteHttpExceptions option to examine full response)"

What is wrong with my code?

var host = 'https://ftx.com';
var endpoint ='/api/wallet/all_balances';
var url = host   endpoint;

var timestamp = ''  new Date().getTime();
var payload = timestamp   'GET'   endpoint '';

var shaObj = new jsSHA("SHA-256", "BYTES");
shaObj.setHMACKey(api_secret, "BYTES");
shaObj.update(payload);
var signature = shaObj.getHMAC("HEX");

var options = {
 method: 'get',
 headers: {
  'FTX-KEY': api_key,
  'FTX-TS': timestamp,
  'FTX-SIGN': signature
  },
 muteHTTPExceptions: 'true'
}

var jsondata = UrlFetchApp.fetch(url, options);
var data   = JSON.parse(jsondata.getContentText());

CodePudding user response:

I believe your goal is as follows.

  • You want to request the Get balances of all accounts of FTX API using Google Apps Script.

  • The sample python script for retrieving the request header is as follows.

      import time
      import hmac
      from requests import Request
    
      ts = int(time.time() * 1000)
      request = Request('GET', '<api_endpoint>')
      prepared = request.prepare()
      signature_payload = f'{ts}{prepared.method}{prepared.path_url}'.encode()
      signature = hmac.new('YOUR_API_SECRET'.encode(), signature_payload, 'sha256').hexdigest()
    
      request.headers['FTX-KEY'] = 'YOUR_API_KEY'
      request.headers['FTX-SIGN'] = signature
      request.headers['FTX-TS'] = str(ts)
    

In this case, when your script is modified, how about the following modification?

Modified script:

Unfortunately, new jsSHA() cannot be directly used. At Google Apps Script, there is the Class Utilities. You can use the method "computeHmacSha256Signature" of this Class. And, I think that muteHTTPExceptions: 'true' is muteHttpExceptions: true.

function myFunction() {
  var api_key = "YOUR_API_KEY"; // Please set your API key.
  var secret = "YOUR_API_SECRET"; // Please set your secret value.

  var host = 'https://ftx.com';
  var endpoint = '/api/wallet/all_balances';
  var url = host   endpoint;
  var timestamp = ''   new Date().getTime();
  var payload = timestamp   'GET'   endpoint   '';
  var signature = Utilities.computeHmacSha256Signature(payload, secret).map(byte => ('0'   (byte & 0xFF).toString(16)).slice(-2)).join('');
  var options = {
    method: 'get',
    headers: {
      'FTX-KEY': api_key,
      'FTX-TS': timestamp,
      'FTX-SIGN': signature
    },
    muteHttpExceptions: true
  }
  var res = UrlFetchApp.fetch(url, options);
  console.log(res.getContentText())
}

References:

  • Related