我发现了导致这个问题的两个方面,尽管它花费了很多 试错法和调试。
的 OPTIONS对象格式 强>
我认为主要问题是'选项'对象的格式。我认为它需要主要元素“方法”:“标题”:和“有效负载”:而不是 “身体”和“数据”元素。
的 Authoriziation 强> 授权方面花了我很多年才弄明白。 对于像这样的小应用程序使用个人访问令牌方法。 重要的是使用参数“Bearer”+ PERSONAL_ACCESS_TOKEN在标题中使用Authorization选项
PERSONAL_ACCESS_TOKEN是 的 究竟 强> 在注册个人访问令牌时在Asana Web应用程序中提供给您的字符串。它不需要任何进一步的授权/交换/ OAuths /刷新,也不需要在base 64或任何冒号中进行任何编码。
的 调试 强>
我用过Postman( https://www.getpostman.com/ )和体式开发者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; } };
我有一些麻烦正在做你想要做的事情,并且无法将其他代码发布在这里工作,但我终于明白了。无法准确记住我所改变的内容,但您可以轻松使用此功能创建任务并获取其ID:
码:
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; }
尝试对你的身体进行字符串化,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);
将作为评论发布,但代码在那里是不可兑换的。