我发现了导致问题的两个方面,尽管它需要大量的反复试验和调试。
OPTIONS 对象格式
我认为主要问题是“选项”对象的格式。我认为它需要具有主要元素“方法”:“标题”:和“有效负载”:而不是“正文”和“数据”元素。
授权
授权方面花了我很长时间才弄清楚。对于像这样的小型应用程序,请使用个人访问令牌方法。重要的是在标头中使用 Authorization 选项,参数为“Bearer” + PERSONAL_ACCESS_TOKEN
PERSONAL_ACCESS_TOKEN正是注册个人访问令牌时在 Asana Web 应用程序中提供给您的字符串。它不需要任何进一步的授权/交换/OAuths/刷新,也不需要任何 base 64 编码或任何冒号。
调试
我使用 Postman ( https://www.getpostman.com/ ) 和 asana 开发人员 API 参考中的资源管理器来测试这些选项的工作原理,尤其是在授权方面。
我还设置了一个虚拟函数来创建一个定义的名称任务,这样我就可以在谷歌脚本编辑器中访问调试器。
代码:注意我已经调整了 id 等,所以你必须自己输入。
/*************************
* 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;
}
};