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

7.7k Views Asked by At
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.

2

There are 2 best solutions below

0
On BEST ANSWER

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);
   
);
6
On

While doing some research on the topic through https://clickup.com/api, I stumbled across some code. There are a couple of different ones for different things, I'd recommend the first, JavaScript (as that's whats closest to what your currently doing). In a comment you said it was for editing tasks so that's what this code is aimed for.

javascript

var request = new XMLHttpRequest();

request.open('PUT', 'https://api.clickup.com/api/v1/task/{task_id}');

request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', '"access_token"');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log('Status:', this.status);
    console.log('Headers:', this.getAllResponseHeaders());
    console.log('Body:', this.responseText);
  }
};

var body = {
  'name': 'New Task Name',
  'content': 'New Task Content',
  'assignees': {
    'add': [
      1
    ],
    'rem': [
      2
    ]
  },
  'status': 'Closed',
  'priority': 3,
  'due_date': '1508369194377'
};

request.send(JSON.stringify(body));

curl

curl --include \
     --request PUT \
     --header "Content-Type: application/json" \
     --header "Authorization: "access_token"" \
     --data-binary "{
    \"name\": \"New Task Name\",
    \"content\": \"New Task Content\",
    \"assignees\": {
        \"add\" : [
            1
        ],
        \"rem\" : [
            2
        ]
    },
    \"status\": \"Closed\",
    \"priority\": 3,
    \"due_date\": \"1508369194377\"
}" \
'https://api.clickup.com/api/v1/task/{task_id}'

node.js

var request = require('request');

request({
  method: 'PUT',
  url: 'https://api.clickup.com/api/v1/task/{task_id}',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '\'access_token\''
  },
  body: "{  \"name\": \"New Task Name\",  \"content\": \"New Task Content\",  \"assignees\": {    \"add\": [      1    ],    \"rem\": [      2    ]  },  \"status\": \"Closed\",  \"priority\": 3,  \"due_date\": \"1508369194377\"}"
}, function (error, response, body) {
  console.log('Status:', response.statusCode);
  console.log('Headers:', JSON.stringify(response.headers));
  console.log('Response:', body);
});

This is aimed for production let me know if you need mock server or debugging proxy