Creating Asana tasks from Google Apps Script -


i trying create tasks in asana using google apps scripts. manage read (get method) kind of information asana, when try post creating new task in specific workspace , project, creates task using default values ignoring json data pass.

this code i've been using:

function createtask (taskname, wsid, projectid, asigneeid) {    var encoded = utilities.base64encode(asanakey + ":");    var options = {          "method" : "post",          "headers" : {              "accept": "application/json",              "content-type": "application/json",              "authorization": "basic " + utilities.base64encode(asanakey + ":")          },           "body" : {            "data" : {                "name" : "\"" + taskname + "\"" ,                "asignee" : asigneeid,                "projects" : [projectid],                "workspace" : wsid             }           }      };    try {          var url = "https://app.asana.com/api/1.0/workspaces/" + wsid + "/tasks";          var result = urlfetchapp.fetch(url, options);          var salida = result.getcontenttext();        }      catch (e) {          logger.log(e);          var salida = "";        }    {        return salida;    }  }

i've tried data outside body, workspace outside data, i've changed order create tasks default values. ¿any ideas? thanks

i found 2 aspects causing issue, though took great deal of trial , error , debug.

options object format

i think main issue format of 'options' object. think needs have main elements "method": "headers": , "payload": , not "body" , "data" elements.

authoriziation authorisation aspect took me ages figure out. small apps use personal access token method. important thing use authorization option in header parameter of "bearer " + personal_access_token

the personal_access_token exactly string given in asana web app @ time of registering personal access token. not need further authorisation / exchanges / oauths /refreshes, nor need encoding in base 64 or colons.

debugging

i used postman (https://www.getpostman.com/) , explorer in asana developers api reference test out how options worked, particularly around authorisation.

i setup dummy function create defined name task access debugger in google script editor.

code: note have adjusted ids etc. have put own in.

/*************************  * asana     functions    *  *************************/  // first global constants ... key ids / tokens etc. personal_access_token = "0/d3c41c435b0c3f70b399999952edee5";  // put unique personal access token here workspace_id = "49489999934875"; // put in main workspace key want access (you can copy asana web address) assignee = "jondoe@nomail.com";  // put in e-mail addresss use log asana   // ** testtask() **  useful using debug start point.  "select function" on script editor menu // choose "testtask" debug functionality enabled  function testtask() {     quicktask("a quick task")   };   // ** quicktask(taskname) ** made short function add simple tasks function quicktask(taskname) {     var newtask = {     name: taskname,     workspace: workspace_id,     project: "",       // if have project add add here     assignee: "me"     // me understood asana   };   createasanatask(newtask);  };  /******************************************************************************************  **  createasanatask(task) **  ************************   * creates new asana task information (like task name, project, notes etc.) contained in    *  object 'newtask" passed it.  * 'task' should of format object option pairs match asana task  * key parameters, many or few want.  * e.g.   * var newtask = {  *   name: taskname,  *   workspace: workspace_id,  *   project: "my project",       // if have project add add here  *   assignee: "johndoe@madeupmail.com"     // person task should assigned to.  * }  *  add other info due dates etc.  * returns "task" object containing asana task elements of 1 task created including id.  *************************************************************************************************/       function createasanatask(task) {      // when creating asana task must have @ least workspace id , assignee     // routine checks if defined 1 in argument passed if (task.workspace == null) {     task.workspace=workspace_id     } if (task.assignee == null) {     task.assignee="me";     }   /* first setup  "options" object following key elements:  *  *   method: can get,post typically  *  *   headers: object containing header option pairs  *                    "accept": "application/json",        // accept json format *                    "content-type": "application/json",  //content i'm passing json format *                    "authorization": "bearer " + personal_access_token // authorisation *  authorisation aspect took me ages figure out. *  small apps use personal access token method. *  important thing use authorization option in header  *  parameter of  "bearer " + personal_access_token *  personal_access_token  string given in asana web app @ *  time of registering personal access token.  not need further authorisation / exchanges *  nor needo encoding in base 64 or colon. * *  payload: can object option pairs  required each element created... in case  *           task elements passed function in argument "task" object. *            found doesn't need stringifying or anything.    *        ********************************************************************************************************/        var options = {     "method": "post",     "headers": {         "accept": "application/json",         "content-type": "application/json",         "authorization": "bearer " + personal_access_token                },         "payload": task         };   // using try capture errors  try {                           // set base url appropriate endpoint -                            // case "https://app.asana.com/api/1.0"  plus "/tasks"                           // note workspace id or project id not in base url in payload options                           // use asana api reference requirements each method var url = "https://app.asana.com/api/1.0/tasks";                           // using url of endpoint , options object urlfetch.                           // returns object contains json data structure 'result' variable                            // see below sample structure var result = urlfetchapp.fetch(url, options);                           //  var taskjson = result.getcontenttext(); } catch (e) {         logger.log(e);         return null; } {  // parse result text json format object, "data" element object , return it.  // object containing elements of task.     return json.parse(taskjson).data;     } }; 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -