Add Tag to existing task with AppsScript

tags

#1

Hello everyone, I recently restarted using asana and created some smaller automatisations using the API and Google Spreadsheet (i am a beginner in Apps Script). For the last few days I tried to add a tag to an existing task but I just can’t find a way to make it work…this is a part of my code:

var task = {
      "tag" : "tag-id"
  };
  
  var options = {
    "method" : "PUT",
    "headers" : {"Authorization": bearerToken, "Asana-Enable": "new_rich_text"}, 
    "contentType": 'application/json',
    "payload" : JSON.stringify(task) 
  };
  
  
  try {
    var url = "https://app.asana.com/api/1.0/tasks/task-id/addTag";
    var result = UrlFetchApp.fetch(url, options);
    var reqReturn = result.getContentText();

Can anyone help me or explain to me what I am doing wrong? Thank you in advance and have a nice day


#2

Hwy @Jan_Buhr,

I think that you need to define the task-id at your request url https://app.asana.com/api/1.0/tasks/1234567890/addTag

:slight_smile:


#3

Hi @Diakoptis and thank you for the quick reply. In my productive code I have specified the task-id and the tag-id, but when I execute the function like this:

 var task = {
      "tag" : "672843257392271"
      };
      
      var options = {
    "method" : "PUT",
    "headers" : {"Authorization": bearerToken, "Asana-Enable": "new_rich_text"}, 
    "contentType": 'application/json',
    "payload" : JSON.stringify(task) 
      };
      
      
      try {
    var url = "https://app.asana.com/api/1.0/tasks/648902401325421/addTag";
    var result = UrlFetchApp.fetch(url, options);
    var reqReturn = result.getContentText(); 

I get an 404 Error, sadly I only get a shortened version of the error in my log and I don’t know how to enlarge it. I also tried putting the tag-id in [ ] like used in task creation.


#4

Hey @Jan_Buhr,

Check the following code. To Add a tag in a task you need to make a POST request as documentation explain.

function addTag(){
      var taskId = '123456789';
      var tagId = '987654321';
      var token = "'token'"; //Asana Personal Access Token
      var bearerToken = "Bearer " + token;
      //Request
      var request = {
        data : { 
          tag: tagId
        }
      };
      // Request options
      var options = {
        "method" : "POST",
        "headers" : {"Authorization": bearerToken}, 
        "contentType": 'application/json',
        "payload" : JSON.stringify(request) 
      };
      Logger.log(options);

      try {
        var url = "https://app.asana.com/api/1.0/tasks/" + taskId + "/addTag";
        var result = UrlFetchApp.fetch(url, options);
        var reqReturn = result.getContentText();
        Logger.log(reqReturn);
      } 
      catch (e) {
        Logger.log(e);
      }
    }

#5

Thank you very much, this worked perfectly. I actually missed that part and also I forgot to add data: to the oervall function.


#6

Glad I helped. Happy coding and do great things :))