简体   繁体   中英

Consuming Twitters 1.1 API using OAuth, getting no response and it happens instantly

I am trying to swap my website over to consuming the new Twitter 1.1 API with uses OAuth 1.0a. I am able to get the correct response using a REST client and I am now trying to duplicate that on my website using c#.

I have constructed my headers the appropriate way and I have verified that they are in the correct format for what Twitter is looking for.

The issue I am having is that I do not think I am actually sending the request. I say this because my application returns almost instantly. The request should take a second or so to send at least, and my response has totally empty, with no 401 or 400 status code.

Below I have the code that actually sends the request. I am actually sending the request and if so why am I not getting any status code or anything.

Thanks in advance for the help.

//string url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=MYSCREENNAME&count=2";
string url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
webRequest.Headers.Add("Authorization", authorizationHeaderParams);

try {
    var response = webRequest.GetResponse() as HttpWebResponse;
    if (response != null && response.StatusCode != HttpStatusCode.OK) {
        lblresponse.InnerText = "The request did not complete and returned status code: {0} " + response.StatusCode;
    }
    if (response != null) {
        var reader = new StreamReader(response.GetResponseStream());
        reader.ReadToEnd();
        lblresponse.InnerText += "success";
    }
} catch {
    lblresponse.InnerText += "fail";
}

So yeah this code goes straight to the catch block. My thoughts are I am not actually sending the request, since it takes no time to happen. I know there are some libraries designed to make this easier but I would much rather learn how to do it myself (with the help of you guys).

Thanks.

The request is going to throw an exception in the case of a 400 or 401. So catch System.Web.Exception in the catch block to see if there's a 400 or 401.

catch(System.Web.Exception ex) {
   var errorReponse = (HttpWebResponse)ex.Response;
   var statusCode = errorReponse.StatusCode;
   lblresponse.InnerText += "fail";
}  

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