Add Story To Task


#1

In our Windows application, I am accessing all the comments made to a task and displaying them to the users. They would also like a way to add a comment from our application. I can’t find the exact syntax but I think it’s something like this: “/tasks/{0:Target}/stories”, “POST”. I have no idea what “Target” is.

To get the stories, I am issuing: response = GetResponse(String.Format(“https://app.asana.com/api/1.0/tasks/{0}/stories?opt_pretty”, task));

So I am looking for a complement to that to add a story.


#2

From the API docs - “Target” refers to the Task ID:

See here for more info and an example.


#3

Yes, thank you, I saw that.
What I need help with, if you know, is how to issue that request from my C#/Windows application. It would be the complement to GetResponse.


#4

@MMOCK, here are the docs for commenting on a task (i.e. creating a story).

We don’t have any examples in C#, but the docs do have a cURL request example for making a comment:

curl -H "Authorization: Bearer <personal_access_token>" \
https://app.asana.com/api/1.0/tasks/1001/stories \
--data-urlencode "text=This is a very nice comment."

#5

@MMOCK I’m using the AsanaNet project as the basis for my communications so I don’t have a simple code fragment I could give you; but I think something like the HTTPClient PostAsync example shown here should do the trick. Let us know how that goes.


#6

It went beautifully! Thank you!!!

I can’t recall where I found this function, but it went like this:

    public string GetResponse(string uri, string data = "", string method = "GET")
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        // Create Request
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.PreAuthenticate = true;
        request.Method = method;
        request.ContentType = "application/x-www-form-urlencoded";

        // Log In
        string authInfo = "0/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + ":" + "";
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        request.Headers["Authorization"] = "Basic " + authInfo;

        // send data if supplied
        if (data != "")
        {

        }
        // receive response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        return new StreamReader(response.GetResponseStream()).ReadToEnd();

    }

So then I realized I just need to fill in the part when data is not blank so I can post my new asana comment. I googled “GetResponse(string uri, string data = “”, string method = “GET”)” to see if I could get back to where I found that, and it led me to https://stackoverflow.com/questions/27108264/c-sharp-how-to-properly-make-a-http-web-get-request. That gave me the missing block which is simply:

        // send data if supplied
        if (data != "")
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(data);
            request.ContentLength = dataBytes.Length;
            using (Stream requestBody = request.GetRequestStream())
            {
                requestBody.Write(dataBytes, 0, dataBytes.Length);

            }
        }

And I call into it:

    private void cmdAddStory_Click(object sender, EventArgs e)
    {
        string response;
        response = GetResponse("https://app.asana.com/api/1.0/tasks/xxxxxx/stories", "text=This is my new comment", "POST");
    }

In my response the StatusCode is Created so then I checked asana from the user end and there it is, my new comment!
So thank you, again!