简体   繁体   中英

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!

I believe your current situation and goal as follows.

  • From your question, I understood that the script of Working code: works.
  • You want to use "key": "myTrelloKey" and "token": "myTrelloToken" as the query parameter.

Modification points:

  • At UrlFetchApp.fetch(url, params) , params has no properties of key and token .
    • I think that this is the reason of your issue.
  • Even when these values are used in payload , in that case, the request becomes POST method.
  • When you want to use "key": "myTrelloKey" and "token": "myTrelloToken" as the query parameter, in the current stage, it is required to prepare a script.
    • Although I test whether key and token can be directly used for the request body and the headers instead of the query parameter, unfortunately, these were not succeeded. It seems that these are used as the query parameters. Ref

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM