如何使用Apps脚本检索Asana中的任务(而不是创建)个人访问令牌

时间:2018-02-22 21:38:26

标签: google-apps-script asana asana-api asana-connect

我正在尝试使用Google Apps脚本检索,但不创建来自Asana的任务。

使用Asana API Explore,我构建了一个返回所需数据的网址:https://app.asana.com/api/1.0/tasks?opt_fields=name,assignee_status&assignee=987654321987654&completed_since=2018-02-22&limit=100&workspace=456789123456

此URL以下列格式返回所需数据:

{
  "data": [
    {
      "id": 147258369147258,
      "assignee_status": "inbox",
      "name": "An example task name"
    },
    {
      "id": 963852741963852,
      "assignee_status": "upcoming",
      "name": "And second example task name."
    },
    //etc...
  ]
}

将该URL作为模型,我创建了一个个人访问令牌并在Apps脚本中执行了以下功能:

function getTasks5() {
  // Asana Personal Token
  var bearerToken = "Bearer " + "asdf123456789asdf456789456asdf";

  //Request
  var request = {
    data: {
      opt_fields: ["name", "assignee_status"],
      assignee: "987654321987654",
      completed_since: "2018-02-22",
      limit: "100",
      workspace: "456789123456"
    }
  };

  // Request options
  var options = {
    method: "GET",
    headers: {
      "Authorization": bearerToken
    },
    contentType: "application/json",
    payload: JSON.stringify(request)
  };

  var url = "https://app.asana.com/api/1.0/tasks";
  var result = UrlFetchApp.fetch(url, options);
  var reqReturn = result.getContentText();

  Logger.log(reqReturn);
}

该函数不是像上述URL那样返回所需数据,而是在Asana中创建一个未命名的任务,这是不可取的。它还会返回包含不需要的数据的响应:

{
  "data": {
    "id": 123456789123456,
    "created_at": "2018-02-22T20:59:49.642Z",
    "modified_at": "2018-02-22T20:59:49.642Z",
    "name": "",
    "notes": "",
    "assignee": {
      "id": 987654321987654,
      "name": "My Name Here"
    },
    "completed": false,
    "assignee_status": "inbox",
    "completed_at": null,
    "due_on": null,
    "due_at": null,
    "projects": [],
    "memberships": [],
    "tags": [],
    "workspace": {
      "id": 456789123456,
      "name": "Group Name Here"
    },
    "num_hearts": 0,
    "num_likes": 0,
    "parent": null,
    "hearted": false,
    "hearts": [],
    "followers": [
      {
        "id": 987654321987654,
        "name": "My Name Here"
      }
    ],
    "liked": false,
    "likes": []
  }
}

是否可以简单地以我上面的第一个JSON示例所示的方式获取任务列表而无需创建任务,而无需使用OAuth?如果是这样,需要对Apps Script功能进行哪些更改?

1 个答案:

答案 0 :(得分:1)

好吧,问题在于我采取的方法。我没有使用payload(推断POST请求)格式化请求,而是需要将其更传统地构建为GET请求,如下所示:

var requestUrl = "https://app.asana.com/api/1.0/tasks?opt_fields=name,assignee_status&assignee=123456789123&completed_since=2018-02-22&limit=100&workspace=987654321987";

var headers = {
  "Authorization" : "Bearer " + AUTH_TOKEN
};

var reqParams = {
  method : "GET",
  headers : headers,
  muteHttpExceptions: true
};

然后我能够表演:

UrlFetchApp.fetch(requestUrl, reqParams);

获取我追踪的数据。