简体   繁体   中英

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();

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#)

You could try using my library, AsanaNet :)

https://github.com/acron0/AsanaNet

你需要做HTTP请求,有这么多的教程,如http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.80).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM