Ira
Ira

Reputation: 21

Posting tasks to Asana using API in C#

I'm trying to figure out how to post a new task to a user in asana, but I keep getting the 400 error code. Can anyone tell me what I am doing wrong.

This is what I have so far:

        string apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string ID = "xxxxxxxxxxxxx";
        string url = @"https://app.asana.com/api/1.0/tasks";

        Data dat = new Data();
        dat.workspace = ID;
        dat.name = "Buy eggs";
        dat.notes = "Testing";
        string json = JsonConvert.SerializeObject(dat);
        string data ="\"data\": " + json;

        byte[] bytes = Encoding.UTF8.GetBytes(data);
        var req = (HttpWebRequest)WebRequest.Create(url);
        Console.WriteLine(bytes.ToString());
        req.Method = WebRequestMethods.Http.Post; 
        req.ContentLength = bytes.Length;
        req.ContentType = "application/json";

        var authInfo = apiKey + ":";
        var encodedAuthInfo = Convert.ToBase64String(
            Encoding.Default.GetBytes(authInfo));
        req.Headers.Add("Authorization", "Basic " + encodedAuthInfo);

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

        try
        {
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(res);
            Console.ReadLine();
        }
        catch (WebException ex)
        {
            HttpWebResponse response = ((HttpWebResponse)ex.Response);
            string e = url + " caused a " + (int)response.StatusCode + " error.\n" + response.StatusDescription;
            Console.WriteLine(e);
            Console.ReadLine();
        }

I put the serial converter, did i do it wrong?

Upvotes: 0

Views: 1359

Answers (2)

I have researched a long and found below code useful and working. I can successfully post Task to Asana through my application. I have used Json serializer for dot net. Thanks to Newtonsoft.Json.

        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;
        Task TaskData = new Task();

        try
        {
            authInfo = apiKey + Convert.ToString(":");

            TaskData.workspace = WorkspaceId;
            TaskData.name = TaskName;
            TaskData.notes = TaskNotes;

            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));
            AsanaObjectId newtask = JsonConvert.DeserializeObject<AsanaObjectId>(finalString);

            return newtask;

        }
        catch (WebException ex)
        {
            HttpWebResponse response = (HttpWebResponse)ex.Response;
            string resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
            MessageBox.Show(resp);
            System.Environment.Exit(0);
        }

Upvotes: 1

agnoster
agnoster

Reputation: 3784

As the first comment points out, you need to serialize the data properly. The final body posted should look something like:

{ "data": { "name": "My Example Task", ... }}

(Except of course with the ... replaced with more fields.)

Upvotes: 1

Related Questions