Created
June 6, 2017 20:05
-
-
Save RenzoF/715517662dcfead9abf32ee3e9684f98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************* | |
* Asana Functions * | |
*************************/ | |
// first Global constants ... Key Ids / tokens etc. | |
PERSONAL_ACCESS_TOKEN = "0/d3c41c435b0c3f70b399999952edee5"; // Put your unique Personal access token here | |
WORKSPACE_ID = "49489999934875"; // Put in the main workspace key you want to access (you can copy from asana web address) | |
ASSIGNEE = "jondoe@nomail.com"; // put in the e-mail addresss you use to log into asana | |
// ** testTask() ** is useful for using as a Debug start point. "select function" on script editor menu | |
// choose "testTask" then debug functionality is enabled | |
function testTask() { | |
quickTask("a quick task") | |
}; | |
// ** quickTask(taskName) ** Made a short function so I could just add simple tasks easily | |
function quickTask(taskName) { | |
var newTask = { | |
name: taskName, | |
workspace: WORKSPACE_ID, | |
project: "", // if you have a project you like to add add it here | |
assignee: "me" // Me is understood by asana | |
}; | |
createAsanaTask(newTask); | |
}; | |
/****************************************************************************************** | |
** createAsanaTask(task) ** | |
************************ | |
* creates a new asana task with information (like task name, project, notes etc.) contained in | |
* the object 'newTask" passed to it. | |
* 'task' should be of the format an object with option pairs that match the Asana task | |
* key parameters, as many or as few as you want. | |
* e.g. | |
* var newTask = { | |
* name: taskName, | |
* workspace: WORKSPACE_ID, | |
* project: "My Project", // if you have a project you like to add add it here | |
* assignee: "JohnDoe@madeupmail.com" // person the task should be assigned to. | |
* } | |
* you could add other info like due dates etc. | |
* it returns a "task" object containing all asana task elements of the one task created including the id. | |
*************************************************************************************************/ | |
function createAsanaTask(task) { | |
// when creating an Asana task you must have at least a workspace id and an assignee | |
// this routine checks if you defined one in the argument you passed | |
if (task.workspace == null) { | |
task.workspace=WORKSPACE_ID | |
} | |
if (task.assignee == null) { | |
task.assignee="me"; | |
} | |
/* first setup the "options" object with the following key elements: | |
* | |
* method: can be GET,POST typically | |
* | |
* headers: object containing header option pairs | |
* "Accept": "application/json", // accept JSON format | |
* "Content-Type": "application/json", //content I'm passing is JSON format | |
* "Authorization": "Bearer " + PERSONAL_ACCESS_TOKEN // authorisation | |
* 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 | |
* NOR does it needo any encoding in base 64 or any colon. | |
* | |
* payload: this can be an object with option pairs required for each element to be created... in this case | |
* its the task elements as passed to this function in the argument "task" object. | |
* I found it 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 to capture errors | |
try { | |
// set the base url to appropriate endpoint - | |
// this case is "https://app.asana.com/api/1.0" plus "/tasks" | |
// note workspace id or project id not in base url as they are in the payload options | |
// use asana API reference for the requirements for each method | |
var url = "https://app.asana.com/api/1.0/tasks"; | |
// using url of endpoint and options object do a urlfetch. | |
// this returns an object that contains JSON data structure into the 'result' variable | |
// see below for sample structure | |
var result = UrlFetchApp.fetch(url, options); | |
// | |
var taskJSON = result.getContentText(); | |
} catch (e) { | |
Logger.log(e); | |
return null; | |
} finally { | |
// parse the result text with JSON format to get object, then get the "data" element from that object and return it. | |
// this will be an object containing all the elements of the task. | |
return JSON.parse(taskJSON).data; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment