user1356379
user1356379

Reputation: 21

How do I connect to the Asana Rest API using c#?

Does anyone have a snippet of code for connecting to the Asana API using c#?

There is a Hello World application on their site but unfortunately it is written in ruby.

https://asana.com/developers/documentation/examples-tutorials/hello-world

I'm doing this as a quick side project and can only dedicate a small amount of time to it. Any help would be greatly appreciated.

Thanks in advance!


Follow up:

I've tried multiple ways of accessing/authentication and I keep getting a 401 Not Authorized error.

My latest code looks like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://app.asana.com/api/1.0/users/me");
request.Method = "GET";
request.Headers.Add("Authorization: Basic " + "*MyUniqueAPIKey*");

request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/json";

WebResponse ws = request.GetResponse();

Upvotes: 2

Views: 5332

Answers (3)

Antony Woods
Antony Woods

Reputation: 4462

You could try using my library, AsanaNet :)

https://github.com/acron0/AsanaNet

Upvotes: 3

Ryan Lundy
Ryan Lundy

Reputation: 210140

Try this code. I was able to get a list of users with it:

const string apiKey = "whateverYourApiKeyIs";

public string GetUsers()
{
    var req = WebRequest.Create("https://app.asana.com/api/1.0/users");
    SetBasicAuthentication(req);
    return new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
}

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

This just returns the data as a string. Parsing the JSON is left as an exercise for the reader. ;)

I borrowed the SetBasicAuthentication method from here:

Forcing basic http authentication for HttpWebRequest (in .NET/C#)

Upvotes: 5

Tearsdontfalls
Tearsdontfalls

Reputation: 777

You need to do HTTP requests, there are so many tutorials like this one http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.80).aspx

Upvotes: 0

Related Questions