如何使用c#连接Asana Rest API?

use*_*379 2 .net c# asana

有没有人使用c#连接到Asana API的代码片段?

他们的网站上有一个Hello World应用程序,但不幸的是它是用ruby编写的.

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

我这样做是一个快速的项目,只能花费少量的时间.任何帮助将不胜感激.

提前致谢!


跟进:

我尝试了多种访问/身份验证方式,并且一直收到401 Not Authorized错误.

我的最新代码如下所示:

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();
Run Code Online (Sandbox Code Playgroud)

Rya*_*ndy 5

试试这个代码.我能够得到一个用户列表:

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);
}
Run Code Online (Sandbox Code Playgroud)

这只是将数据作为字符串返回.解析JSON留给读者练习.;)

SetBasicAuthentication从这里借用了这个方法:

强制HttpWebRequest的基本http身份验证(在.NET/C#中)