0

我正在尝试使用 Google Apps 脚本从 Asana检索不创建任务。

使用Asana API Explore,我构建了一个返回所需数据的 URL: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 脚本功能进行哪些更改?

4

1 回答 1

1

好吧,问题出在我采用的方法上。payload我需要将其更传统地构造为 GET 请求,而不是使用 a (推断 POST 请求)来格式化请求,如下所示:

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);

并获得我所追求的数据。

于 2018-02-22T22:21:26.577 回答