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
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
Related
I have a problem with following a user on Instagram with InstaSharp
Here is my code:
private async void Follow()
{
var followMe = await api.FollowUserAsync(userID);
if (followMe.Succeeded)
{
MessageBox.Show("Followed");
}
if (!followMe.Succeeded)
{
MessageBox.Show(followMe.Info.Message);
}
}
And when I call this method in the messageBox it says feedback_required.
How can I fix this?
Also : other functions like Unfollow Login LogOut are working fine I just have problem with Follow function.
Some specific countries ip's will banned in situation like this as you said.
You can use proxies inside your program for this problem if your customers are from those countries.
C# Connecting Through Proxy
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("[ultimate destination of your request]");
WebProxy myproxy = new WebProxy("[your proxy address]", [your proxy port number]);
myproxy.BypassProxyOnLocal = false;
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
I hope this helps you.
I fixed the issue by changing the proxy server. It seems Instagram was banning my IP for no reason!
I wrote a dll using HttpClient and tested it. It was working fine and then suddenly stopped working and instead of sending application/json started to send text/plain. In any case, I was advised to use HttpWebRequest instead of HttpClient. So I am trying to change my working code to that form. I also asked that question here https://social.msdn.microsoft.com/Forums/vstudio/en-US/e396d8ff-5260-42cd-aa5a-0a220d8bf123/switching-from-httpclient-to-webclient?forum=csharpgeneral
My problem is the following. I have host URL https://someSite.com and I also have two URLs which implement the API I need in the form of url = "/integrationapi//Attraction/Tickets/Validate"
I am not sure how should I create my request variable. I have a separate method which I call before attempting to get a response, e.g.
private void SetRequestHeaders(string tcHost, string tcUserName, string tcPassword, int tnTimeout)
{
request = WebRequest.Create(tcHost) as HttpWebRequest;
request.Host = tcHost;
request.Headers.Remove("Accept");
request.Headers.Add("Accept", "application/json");
string authorizationKey = Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", tcUserName, tcPassword)));
request.Headers.Add("Authorization", "Basic " + authorizationKey);
request.Timeout = tnTimeout * 1000;
}
Now, how should I use that request variable to call the API Url I need?
I need to access www.skyscanner.com and get the answer to search (set in console application )
I try
var url= #"www.skyscanner.com";
var webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
using (var streamWriter = new StreamWriter(webRequestLogin.GetRequestStream()))
{
var httpResponsee = (HttpWebResponse)webRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponsee.GetResponseStream()))
{
var response = streamReader.ReadToEnd();
}
}
But i have one error "500", how i can access the site and make a search and get the result?.
Thank's
It doesn't look like your POST request has any payload. Skyscanner's server is probably expecting the itinerary details and since it is not getting them it is throwing the 500 error.
I have to add that what you are doing is not the proper way to interact with Skyscanner's service. They have an official API available for which you will need to register and get an API key here: http://business.skyscanner.net/portal/en-GB/AffiliateNetwork
You will then be able to make your application send requests to http://partners.api.skyscanner.net/apiservices/pricing/v1.0, as documented here:
http://business.skyscanner.net/portal/en-GB/Documentation/FlightsLivePricingList
Can any one guide me how to tackle with Linkedin Invitation API in c# asp.net. I want to send invitation to particular user from my app via this api, but don't find any sufficient information to start it. Can any one give me some example to start with. I have already get list of users from linkedin search api. Now i want to send invitation to those users.
Thanks in advanced. Hope best answer will come out.
Thanks
Finally, it's done. I have successfully implement linkedin invitation api with asp.net c#.
I am posting sample code here, for other users who want to implement it.
try
{
string uid=uniqueid of user,to whom you want to send request.
// if you get this user bysearch api or 1st connection, then from http-header response, you will find value field. split this value by ':' and store in two variable
string name=splitvalue1;
string namevalue=splitvalue2;
string xml = "<?xml version='1.0' encoding='UTF-8'?><mailbox-item><recipients><recipient><person path=\"/people/id=" + uid + "\" /></recipient></recipients>";
xml += "<subject>Invitation to Connect</subject>";
xml += "<body>Please join my professional network on LinkedIn.</body>";
xml += "<item-content><invitation-request><connect-type>friend</connect-type><authorization><name>" + name + "</name><value>" + namevalue + "</value></authorization></invitation-request></item-content></mailbox-item>";
string accessCodeUri = "https://api.linkedin.com/v1/people/~/mailbox?oauth2_access_token=" + Session["accessCode"]; // this is session value which you get on authorization success return by linkedin
WebRequest request = WebRequest.Create(accessCodeUri);
request.Method = "POST";
request.ContentType = "application/xml";
request.ContentLength = xml.Length;
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
requestWriter.Write(xml);
requestWriter.Close();
WebResponse webResponse = request.GetResponse();
//success
}
catch(WebException exc)
{
}
Hope it helps other.
I am having difficulty in consuming the reCaptcha Web Service using C#/.Net 3.5. Although I think the problem is with consuming web services in general.
String validate = String.Format("http://api-verify.recaptcha.net/verify?privatekey={0}&remoteip={1}&challenge={2}&response={3}", PrivateKey, UserIP, Challenge, Response);
WebClient serviceRequest = new WebClient();
serviceRequest.Headers.Add("ContentType","application/x-www-form-urlencoded")
String response = serviceRequest.DownloadString(new Uri(validate ));
It keeps telling me that the error is: nverify-params-incorrect. Which means:
The parameters to /verify were incorrect, make sure you are passing all the required parameters.
But it's correct. I am using the private key, the IP address (locally) is 127.0.0.1, and the challenge and response seem fine. However the error keeps occurring.
I am pretty sure this is a issue with how I am requesting the service as this is the first time I have actually used webservices and .Net.
I also tried this as it ensures the data is posted:
String queryString = String.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}",PrivateKey, UserIP, Challenge, Response);
String Validate = "http://api-verify.recaptcha.net/verify" + queryString;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Validate));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Validate.Length;
**HttpWebResponse captchaResponse = (HttpWebResponse)request.GetResponse();**
String response;
using (StreamReader reader = new StreamReader(captchaResponse.GetResponseStream()))
response = reader.ReadToEnd();
Seems to stall at the point where I get response.
Any advice?
Thanks in advance
Haven't worked with the recaptcha service previously, but I have two troubleshooting recommendations:
Use Fiddler or Firebug and watch what you're sending outbound. Verifying your parameters would help you with basic troubleshooting, i.e. invalid characters, etc.
The Recaptcha Wiki has an entry about dealing with development on Vista. It doesn't have to be limited to Vista, though; if you're system can handle IPv6, then your browser could be communicating in that format as a default. It appears as if Recaptcha deals with IPv4. Having Fiddler/Firebug working would tell you about those other parameters that could be causing you grief.
This may not help solve your problem but it might provide you with better troubleshooting info.
So got this working, for some reason I needed to write the request to a stream like so:
//Write data to request stream
using (Stream requestSteam = request.GetRequestStream())
requestSteam.Write(byteData, 0, byteData.Length);
Could anyone explain why this works. I didn't think I would need to do this, don't completely understand what's happening behind the scenes..
Damien's answer is correct of course, but just to be clear about the order of things (I was a little confused) and to have a complete code sample...
var uri = new Uri("http://api-verify.recaptcha.net/verify");
var queryString = string.Format(
"privatekey={0}&remoteip={1}&challenge={2}&response={3}",
privateKey,
userIP,
challenge,
response);
var request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = queryString.Length;
request.ContentType = "application/x-www-form-urlencoded";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(queryString);
}
string result;
using (var webResponse = (HttpWebResponse)request.GetResponse())
{
var reader = new StreamReader(webResponse.GetResponseStream());
result = reader.ReadToEnd();
}
There's a slight difference in that I'm writing the post variables to the request, but the core of it is the same.