I am trying to use the Asana API to update and add data to Asana. How do I do it in C#?
I am getting data fine - sample code below:
string apiKey = "xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxx";
var req = WebRequest.Create("https://app.asana.com/api/1.0/workspaces/xxxxxxxxxxxxxxx/projects");
var authInfo = apiKey + ":";
var encodedAuthInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers.Add("Authorization", "Basic " + encodedAuthInfo);
var response = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
But I don't know how to POST data.
All the examples in their documentation is using Python which I have no experience with.
I have contacted Asana but have yet to hear back. This is what I have so far. I get a 400 error on the last line
var url = "https://app.asana.com/api/1.0/workspaces/xxxxxxxxxxxxxxxxxxx/tasks";
string json =
"\"data\": { " +
"\"workspace\": nnnnnnnnnnnnnnnn," +
"\"name\": \"test\"," +
"\"notes\": \"testing API POST\"" +
"}";
byte[] bytes = Encoding.UTF8.GetBytes(json);
var req = WebRequest.Create(url) as HttpWebRequest;
req.Method = "POST";
req.ContentLength = bytes.Length;
req.ContentType = "application/json";
var requestStream = req.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
var response = req.GetResponse(); //error
This is how I do it:
My WebRequestModel looks like this:
This is what I use specifically to interact with the Asana API. The encode parameters function takes a list of key value pairs and converts them into a string of the form "key=value&key=value".
The send data portion of the code is not mine, but I can't remember where I got it from.
I just started developing an API wrapper in C#, so this is very lightly tested. It may not be as robust as it could be, but I know for a fact that it does work well enough to create a new workspace in Asana.