我正在尝试使用谷歌应用脚本在Asana中创建任务。 我设法从asana读取(GET方法)任何类型的信息,但是当我尝试执行POST时,如在特定工作空间和项目中创建新任务时,它会创建任务但使用默认值忽略我传递的json数据
这是我一直在使用的代码:
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;
}
}
我已经尝试过外部数据,工作区外部数据,我已经更改了顺序,但它始终使用默认值创建任务。 有任何想法吗? 感谢
答案 0 :(得分:2)
我发现导致这个问题的两个方面,虽然花了很多 试错法和调试。
OPTIONS对象格式
我认为主要问题是'选项的格式'宾语。我认为它需要有主要元素"方法":"标题":和"有效负载":而不是 "身体"和"数据"元件。
<强> Authoriziation 强> 授权方面花了我很多年才弄明白。 对于像这样的小应用程序使用个人访问令牌方法。 重要的是在标题中使用授权选项,参数为&#34; Bearer&#34; + PERSONAL_ACCESS_TOKEN
PERSONAL_ACCESS_TOKEN 完全在注册个人访问令牌时在Asana Web应用程序中为您提供的字符串。它不需要任何进一步的授权/交换/ OAuths /刷新,也不需要在base 64或任何冒号中进行任何编码。
<强>调试强>
我使用Postman(https://www.getpostman.com/)和asana开发人员API参考中的资源管理器来测试选项的工作方式,特别是在授权方面。
我还设置了一个虚拟函数来创建一个已定义的名称任务,因此我可以在谷歌脚本编辑器中访问调试器。
CODE: 注意我已经调整了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;
}
};
答案 1 :(得分:0)
尝试对您的身体进行字符串化,google也使用方法有效负载,如果这适用于所有REST请求,则为dunno:
var options = {
"method" : "POST",
"headers" : {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Basic " + Utilities.base64Encode(asanaKey + ":")
}
}
var body = {
"data" : {
"name" : "\"" + taskName + "\"" ,
"asignee" : asigneeId,
"projects" : [projectId],
"workspace" : wsId
}
};
options.payload = JSON.stringify(body);
会发表评论,但代码在那里是无法解决的。
答案 2 :(得分:0)
我做了一些你正在尝试做的事情并且无法将其他代码发布到这里工作,但我终于明白了。无法准确记住我更改的内容,但您可以轻松使用此功能创建任务并获取其ID:
CODE:
function testTask(){
var taskName = "Test Task 2"; //Task Name
var wsId = "44492991234567"; //Workspace ID
var projectId = "9120451234567"; //Project ID
var assigneeId = "44645081234567"; //Assignee ID
var parentId = null; //Parent ID, can be null if no parent
createTask(taskName, wsId, projectId, assigneeId, parentId);
}
function createTask (taskName, wsId, projectId, assigneeId, parentId) {
var token = “access token goes here"; //your asana Personal Access Token
var bearerToken = "Bearer " + token;
var task = {
data: {
assignee : 'me',
notes : 'test',
workspace: wsId,
name : taskName,
projects : [projectId],
parent: parentId
}
};
var options = {
"method" : "POST",
"headers" : {"Authorization": bearerToken},
"contentType": 'application/json',
"payload" : JSON.stringify(task)
};
try {
var url = "https://app.asana.com/api/1.0/tasks";
var result = UrlFetchApp.fetch(url, options);
var reqReturn = result.getContentText();
Logger.log(reqReturn);
var createdTaskId = JSON.stringify(JSON.parse(reqReturn).data.id);
Logger.log(createdTaskId);
}
catch (e) {
Logger.log(e);
}
return createdTaskId;
}