Search code examples
google-apps-scriptgeturlfetchclickup-api

Using my personal clickup token to make a clickup api call from Google Apps Script


function getClickupTeam() {
  let response = UrlFetchApp.fetch(clickupUrl + "team", {
    "method": "GET",
    "Authorization": clickupToken,
    "muteHttpExceptions": true
  })
  Logger.log(response)
  let json = JSON.parse(response);
  Logger.log(json);
}

This URLFetchApp call returns {ECODE=OAUTH_017, err=Authorization header required} even though I am including my personal clickup token in the request. Am I missing something? Any help would be appreciated.


Solution

  • It looks like your request is malformed (be sure to check out the reference documentation for the UrlFetchApp.fetch(url, params) method.) The Authorization header should be in an explicit headers object. Plus, you don't need to set method to GET since its the default.

    Something else to keep in mind for when you're making POST requests - Google Apps Script has this funny quirk where you have to define the Content-Type header using the contentType property. If you try to set that header in the headers object if will just get overridden by the default (application/x-www-form-urlencoded I believe).

    So here's how you'd set up your GET request:

    function getClickupTeam() {
        let response = UrlFetchApp.fetch(clickupUrl + "team", {
            "muteHttpExceptions": true,
            "headers": {
                "Authorization": clickupToken
            }
        }
        
        console.log(response.getContentText());
        let json = JSON.parse(response.getContentText());
        
        console.log(json);
       
    );
    

    And for POST requests with a JSON payload you'd do something like this:

    function getClickupTeam() {
        let response = UrlFetchApp.fetch(clickupUrl + "team", {
            "method": "POST",
            "contentType": "application/json",
            "muteHttpExceptions": true,
            "headers": {
                "Authorization": clickupToken
            },
            "payload": JSON.stringify({
                "key": "value"
            });
        }
        
        console.log(response.getContentText());
        let json = JSON.parse(response.getContentText());
        
        console.log(json);
       
    );