google-apps-scriptgoogle-sheetstrellotrello-powerup

Google Script to send GET request to Trello API and import data to Google Sheet


I have a small script to pull information from a Trello API.

The script works fine when I've assigned the full URL to a String variable. However, when I pass the params separately, I get an error "unauthorized permission requested".

Working code:

var url = "https://api.trello.com/1/boards/57c68c1beaab4c676adfaeb1/lists?key=myTrelloKey&token=myTrelloToken";

  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());

Problematic code:

var url = "https://api.trello.com/1/boards/57c68c1beaab4c676adfaeb1/lists";

  var options = 
 {
  "key": "myTrelloKey",
  "token": "myTrelloToken",
  "muteHttpExceptions" : true
  };
  
  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response.getContentText());

I have tried to understand if it's an Authentication issue, but could not get my way around it. Am I doing something wrong in the second version? Thanks in advance!


Solution

  • I believe your current situation and goal as follows.

    Modification points:

    When above points are reflected to your script, it becomes as follows.

    Modified script:

    function myFunction() {
      // This is from https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
      String.prototype.addQuery = function(obj) {
        return this + Object.keys(obj).reduce(function(p, e, i) {
          return p + (i == 0 ? "?" : "&") +
            (Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
              return str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : "")
            },"") : e + "=" + encodeURIComponent(obj[e]));
        },"");
      }
    
      var url = "https://api.trello.com/1/boards/57c68c1beaab4c676adfaeb1/lists";
      var query = {
        "key": "myTrelloKey",
        "token": "myTrelloToken",
      };
      var endpoint = url.addQuery(query);
      Logger.log(endpoint); // <--- https://api.trello.com/1/boards/57c68c1beaab4c676adfaeb1/lists?key=myTrelloKey&token=myTrelloToken
    
      var response = UrlFetchApp.fetch(endpoint, {"muteHttpExceptions" : true});
      Logger.log(response.getContentText());
    }
    

    References: