0

I'm trying to use C# to make API calls into our Asana project. I can "get" all of the tasks under a specific project with no issues. I can also create "post" a new task without any issues. I'm having difficulty re-purposing this code that was used for the create/post to perform a "put/update" for an existing Task. I had tried to specify as much information as possible when passing over the JSON data, but haven't succeeded in updating. What my main goal is, simply to be able to mark a task completed via the API. If anyone has any suggestions/recommendations on what could be preventing me from a successful Update/Put call to an Asana Task that would be awesome. Thanks everyone for your time.

Update Code - Not Working

public static bool UpdateTask(string taskId)
    {
        string json = null;
        byte[] bytes = null;
        string url = "https://app.asana.com/api/1.0/tasks";
        HttpWebRequest req = default(HttpWebRequest);
        Stream reqStream = default(Stream);
        string authInfo = null;
        AsanaUpdateTask TaskData = new AsanaUpdateTask();
        try
        {
            authInfo = AsanaApiKey + Convert.ToString(":");

            TaskData.task = taskId;
            TaskData.name = "Business Collaboration";
            TaskData.assignee = "me";
            TaskData.assignee_status = "upcoming";
            TaskData.completed = "true";
            TaskData.due_on = "null";
            TaskData.notes = "new notes from API call 936am";

            json = JsonConvert.SerializeObject(TaskData);
            json = Convert.ToString("{ \"data\":") + json + "}";

            bytes = Encoding.UTF8.GetBytes(json);

            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;

            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = WebRequestMethods.Http.Put;
            req.ContentType = "application/json";
            req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)));
            req.ContentLength = bytes.Length;

            reqStream = req.GetRequestStream();
            reqStream.Write(bytes, 0, bytes.Length);
            reqStream.Close();


            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(res);
            Console.ReadLine();

            string finalString = res.Remove(0, 8);
            finalString = finalString.Remove((finalString.Length - 1));
            var newtask = JsonConvert.DeserializeObject(finalString);

            return true;
        }
        catch (WebException ex)
        {
            HttpWebResponse response = (HttpWebResponse)ex.Response;
            string resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
        }
        return false;
    }

JSON Data for Update

{ "data":{"task":"156272920352856","name":"Business Collaboration","assignee":"me","assignee_status":"upcoming","completed":"true","due_on":"null","notes":"new notes from API call 936am"}}

Create/Post Task Code - Currently Working

public static void CreateTask(string workspaceId, string projectId)
{
    string json = null;
    byte[] bytes = null;
    string url = "https://app.asana.com/api/1.0/tasks";
    HttpWebRequest req = default(HttpWebRequest);
    Stream reqStream = default(Stream);
    string authInfo = null;
    AsanaCreateTask TaskData = new AsanaCreateTask();
    try
    {
        authInfo = AsanaApiKey + Convert.ToString(":");
        TaskData.workspace = workspaceId;
        TaskData.name = "Business Collaboration";
        TaskData.notes = "new api notes 947am";

        json = JsonConvert.SerializeObject(TaskData);
        json = json.Insert((json.Length - 1), ",\"projects\":[" + projectId + "]");
        json = Convert.ToString("{ \"data\":") + json + "}";

        bytes = Encoding.UTF8.GetBytes(json);

        req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = WebRequestMethods.Http.Post;
        req.ContentType = "application/json";
        req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)));
        req.ContentLength = bytes.Length;

        reqStream = req.GetRequestStream();
        reqStream.Write(bytes, 0, bytes.Length);
        reqStream.Close();


        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
        Console.WriteLine(res);
        Console.ReadLine();

        string finalString = res.Remove(0, 8);
        finalString = finalString.Remove((finalString.Length - 1));
        var newtask = JsonConvert.DeserializeObject(finalString);
    }
    catch (WebException ex)
    {
        HttpWebResponse response = (HttpWebResponse)ex.Response;
        string resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
    }
}

JSON Data for Create

{ "data":{"notes":"new api notes 956am","workspace":"143129080097680","name":"Business Collaboration","projects":[143129080097684]}}
3
  • What error are you getting when you try?
    – Mark
    Jul 19, 2016 at 1:38
  • @Mark I keep getting a 404 "Not Found".
    – fenix
    Jul 19, 2016 at 15:26
  • 1
    It looks to me like you need to specify the task id when you update directly in the url. .../tasks/<id> instead of the way you are doing it right now.
    – Mark
    Jul 19, 2016 at 17:48

1 Answer 1

1

I took a look at AsanaNet on gitHub and that provided more insight as well as Marks comment regarding direct taskId in URL. Here is a fully working "Update Task" method for Asana. Figured it would help someone else out there one day.

I created a small object that I could serialize/deserialize into JSON.

public class AsanaUpdateTask
{
    public string notes { get; set; }
    public string workspace { get; set; }
    public string name { get; set; }
    public bool completed { get; set; }
}

And here is the method that "puts" the update successfully.

public static void UpdateTask(string workspaceId, string taskId)
{
    string json = null;
    byte[] bytes = null;
    string url = string.Format("https://app.asana.com/api/1.0/tasks/{0}", taskId);
    HttpWebRequest req = default(HttpWebRequest);
    Stream reqStream = default(Stream);
    string authInfo = null;
    AsanaUpdateTask TaskData = new AsanaUpdateTask();
    try
    {
        authInfo = BearerToken + Convert.ToString(":");
        /*Required Minimum for a response from API::: workspace, name, and notes*/
        TaskData.workspace = workspaceId; 
        TaskData.name = "Business Collaboration V7";
        TaskData.notes = "1306:: this is working..updated from api Aug 26";
        TaskData.completed = false; //Used to mark task completed

        json = JsonConvert.SerializeObject(TaskData);
        json = Convert.ToString("{ \"data\":") + json + "}";

        bytes = Encoding.UTF8.GetBytes(json);

        req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = WebRequestMethods.Http.Put;
        req.ContentType = "application/json";
        req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)));
        req.ContentLength = bytes.Length;

        reqStream = req.GetRequestStream();
        reqStream.Write(bytes, 0, bytes.Length);
        reqStream.Close();


        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
        Console.WriteLine(res);
        Console.ReadLine();

        string finalString = res.Remove(0, 8);
        finalString = finalString.Remove((finalString.Length - 1));
        var newtask = JsonConvert.DeserializeObject(finalString);
    }
    catch (WebException ex)
    {
        HttpWebResponse response = (HttpWebResponse)ex.Response;
        string readableString = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.