0
votes

Total noob programmer here (originally an old-school mechanical engineer).

After going through some of Javascript lessons online, I thought I could try buidling a simple script on Google Sheet. My project plan was to have a script to automatically track all of my transactions on Luno.com (cryptoexchange) through their API. Their API documentation is here.

I was attempting to use List Transactions to populate all historical transactions on my spreadsheet, but I got stuck at the authenticate UrlFetchApp.fetch(url) endpoint access request

Edit: You should reconsider sharing your authentication information publicly. I removed it.

Below is my Code.gs script:

function importStatementLuno() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet (bound to this script)
  var url = 'https://api.luno.com/api/1/accounts/xxx/transactions'; //api endpoint as a string

  var response = UrlFetchApp.fetch(url,{'muteHttpExceptions': false}); // get api endpoint
  var json = response.getContentText(); // get the response content as text
  var lunodata = JSON.parse(json); //parse text into json

Logger.log(lunodata);

My fetch response gave me this Error:

Exception: Request failed for https://api.luno.com returned code 401. Truncated server response: Unauthorized (use muteHttpExceptions option to examine full response)

I can't for my life understand their documentation! I'm not sure if there's actually different expressions used to provide authentication keys to their server (they explicitly mentioned they support Go better in their Get Started documentation) I hope someone here could help me point out where I went wrong.

1
You should not share private information on this forum. If the authentication information you provided allows others to to access your account you should probably consider changing it immediately.Cooper
Hi @Zann, were you able to try basic authentication mentioned in the answer below?NaziA

1 Answers

1
votes

Based on the Luno-Go documentation, it uses SetBasicAuth and according to Go documentation, SetBasicAuth is just HTTP Basic Authentication. Can you try the code below if it fixes your issue?

Code:

var apiKeyID = 'apiKeyID';
var apiKeySecret = 'apiKeySecret';

var headers = {
  "Authorization": "Basic " + Utilities.base64Encode(apiKeyID + ":" + apiKeySecret)
};

var options = {
  "headers" : headers 
};

var response = UrlFetchApp.fetch(url, options); // get api endpoint

New Error:

error

  • Error is now different since I used an invalid credential. Try inputting yours here.