从Google Apps脚本创建Asana任务 [英] Creating Asana tasks from Google Apps Script

查看:184
本文介绍了从Google Apps脚本创建Asana任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用谷歌应用程序脚本在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}}};尝试{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 =; }终于{return salida;我已经尝试使用身体外的数据,工作区以外的数据数据,我已经改变了顺序,但它总是使用默认值创建任务。
¿任何想法?
Thanks $ / $>

解决方案

我发现了导致此问题的两个方面,尽管它花费了大量的
试用和错误和调试。

OPTIONS对象格式

我认为主要问题是选项对象的格式。我认为它需要有方法的主要元素:标题和有效载荷:而不是
body和data元素。



授权
授权方面花了我很长时间才弄清楚。对于像这样的小应用程序,
使用Personal Access Token方法。
重要的是使用标题中的授权选项,其参数为承载+ PERSONAL_ACCESS_TOKEN



PERSONAL_ACCESS_TOKEN 正好 Asana Web应用程序在注册Personal Access Token时向您提供的字符串。它不需要任何进一步的授权/交换/ OAuths /刷新,也不需要任何基于64或任何冒号的任何编码。

调试



我使用Postman( https://www.getpostman.com/ )和asana开发者API参考资源中的资源管理器来测试选项如何工作,特别是在授权。

我还设置了一个虚拟函数来创建一个定义的名称任务,以便我可以在谷歌脚本编辑器中访问调试器。



CODE:
注意我已经调整了ID等,因此您必须自己加入。

  / ************************* 
* Asana函数*
************************* /

//第一个全局常量...键ID /标记等
PERSONAL_ACCESS_TOKEN =0 / d3c41c435b0c3f70b399999952edee5; //将您的唯一个人访问令牌放在这里
WORKSPACE_ID =49489999934875; //放入您想要访问的主工作区键(您可以从asana网址复制)
ASSIGNEE =jondoe@nomail.com; //放入用于登录asana的电子邮件地址


// ** testTask()**对于用作调试起点很有用。脚本编辑器菜单中的select function
//选择testTask,然后启用调试功能

function testTask(){
quickTask(a quick task)
};


// ** quickTask(taskName)**创建了一个简短函数,以便轻松添加简单任务
function quickTask(taskName){
var newTask = {
name:taskName,
workspace:WORKSPACE_ID,
project:,//如果您有一个您想添加的项目,请在此处添加它
受理人:我 //我可以通过asana
}理解;
createAsanaTask(newTask);

};

/ ***************************************** ******************************************************** b $ b ** createAsanaTask(任务)**
************************
*创建一个新的asana任务包含在
*对象'newTask'中的信息(如任务名称,项目,注释等)
*'task'的格式应该是一个对象,
* eg
* var newTask = {
* name:taskName,
* workspace:WORKSPACE_ID任务
*关键参数,
*项目:我的项目,//如果您有一个您想添加的项目,请在此处添加它
*受理人:JohnDoe@madeupmail.com//将任务分配给的人员。
*}
*您可以添加其他信息,例如截止日期等。
*它返回一个task对象,其中包含创建的一个任务的所有asana任务元素,包括id。
************************************************ ***************** ******************************** /



功能createAsanaTask(任务){

//创建一个Asana任务时,您必须至少有一个工作区ID和一个受让人
//此例程检查您是否在您传递的参数中定义了一个
if(task.workspace == null){
task.workspace = WORKSPACE_ID
}
if(task.assignee == null){
task.assignee =我;
}

/ *首先用以下关键元素设置options对象:
*
*方法:可以是GET,POST通常是
*
* headers:包含header选项对象的对象
*Accept:application / json,//接受JSON格式
*Content-Type:application / json, //我传递的内容是JSON格式
*授权:承载者+ PERSONAL_ACCESS_TOKEN //授权
*授权方面花了我很长时间才弄清楚。对于像这样的小应用程序,
*使用Personal Access Token方法。
*重要的是在标头中使用
*参数的授权选项+ PERSONAL_ACCESS_TOKEN
* PERSONAL_ACCESS_TOKEN恰好是在Asana Web中为您提供的字符串应用程序在
*注册个人访问令牌的时间。它不需要任何进一步的授权/交换
* NOR是否需要基于64位或任何冒号的任何编码。
*
*有效载荷:这可以是每个要创建的元素所需的选项对的对象...在这种情况下,
*其任务元素在参数中传递给此函数任务对象。
*我发现它不需要字符串化或任何东西。
*
***************************************** ************************************************** ************* /

var options = {
method:POST,
headers:{
Accept:application / json,
Content-Type:application / json,
Authorization:Bearer+ PERSONAL_ACCESS_TOKEN
},
有效载荷:任务
};
//使用尝试捕获错误
尝试{
//将基本网址设置为适当的端点 -
//这种情况是https://app.asana.com /api/1.0plus/ tasks
//注意工作空间标识或项目标识不在基础URL中,因为它们位于有效负载选项中
//使用asana API参考以获取每种方法的要求
var url =https://app.asana.com/api/1.0/tasks;
//使用端点和选项对象的url做一个urlfetch。
//这会将包含JSON数据结构的对象返回到'result'变量中
//参见下面的示例结构
var result = UrlFetchApp.fetch(url,options);
//
var taskJSON = result.getContentText();
} catch(e){
Logger.log(e);
返回null;
} finally {
//用JSON格式解析结果文本以获取对象,然后从该对象获取data元素并将其返回。
//这将是一个包含任务所有元素的对象。
返回JSON.parse(taskJSON).data;
}
};


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.

/*************************
 * 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;
    }
};

这篇关于从Google Apps脚本创建Asana任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆