问题
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