google-apps-scriptasanaasana-apiasana-connect

How to Retrieve (not create) Tasks from Asana using Apps Script & Personal Access Token


I am attempting to retrieve, but not create, tasks from Asana using Google Apps Script.

Using the Asana API Explore, I have constructed a URL that returns the data I desire: https://app.asana.com/api/1.0/tasks?opt_fields=name,assignee_status&assignee=987654321987654&completed_since=2018-02-22&limit=100&workspace=456789123456

This URL returns the desired data, in the following format:

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

With that URL as a model, I have created a Personal Access Token and executed the following function within Apps Script:

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

Instead of returning the desired data as the aforementioned URL does, the function creates an unnamed task in Asana, which is undesirable. It also returns this response containing undesired data:

{
  "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": []
  }
}

Is it possible to simply GET a list of tasks in the manner exemplified by my first JSON example above without creating a task, and without resorting to using OAuth? If so, what changes to the Apps Script function need to be made?


Solution

  • Alright, the problem was with the approach I was taking. Rather than format the request with a payload (which infers a POST request), I needed to structure it more traditionally as a GET request, like so:

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

    Then I was able to perform:

    UrlFetchApp.fetch(requestUrl, reqParams);
    

    And obtain the data I was after.