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

此生再无相见时 提交于 2019-12-11 03:29:33

问题


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

回答1:


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




回答2:


You could try using my library, AsanaNet :)

https://github.com/acron0/AsanaNet




回答3:


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



来源:https://stackoverflow.com/questions/10317171/how-do-i-connect-to-the-asana-rest-api-using-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!