I am trying to create tasks in Asana using google apps scripts. I do manage to read (GET method) any kind of information from asana, but when I try to do a POST like creating a new task in a specific workspace and project, it creates the task but using default values ignoring json data that I pass.
this is the 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 = "";
}
finally {
return salida;
}
}
I've tried with data outside body, workspace outside data, i've changed the order but it always create tasks with default values. ¿any ideas? Thanks
I found two aspects that were causing the issue, though it took a great deal of trial and error and debug.
OPTIONS object format
I think the main issue is the format of the 'options' object. I think it needs to have the main elements "method": "headers": and "payload": and not the "body" and "data" elements.
Authoriziation The authorisation aspect took me ages to figure out. for small apps like this use the Personal Access Token method. the important thing is to use the Authorization option in the header with the parameter of "Bearer " + PERSONAL_ACCESS_TOKEN
The PERSONAL_ACCESS_TOKEN is exactly the string as given to you in the Asana Web app at the time of registering a Personal Access Token. it DOES NOT need any further authorisation / exchanges / OAuths /refreshes, nor does it need any encoding in base 64 or any colons.
Debugging
I used Postman (https://www.getpostman.com/) and the explorer in the asana developers API reference to test out how the options worked, particularly around the authorisation.
I also setup a dummy function to create a defined name task so I could access the debugger in the google script editor.
CODE: Note I have adjusted the ids etc. so you have to put your own in.
I had a few troubles doing what you are trying to do and couldn't get the other codes posted here to work, but i finally figured it out. Can't exactly remember what i changed, but you can easily use this function to create a task and get its id:
CODE:
Try stringifying your body, also, google uses the method payload, dunno if this is applicable with all REST requests:
Was gonna post as a comment, but code is unreadeable there.