C# - How to authenticate a GET call in REST API v1.1?



Like many others, our application stopped working yesterday when the new version of the API was released and one of our GET methods wasn’t authenticated.

I’m just trying to figure out how to authenticate it. I know all four inputs are required (consumer key, access token and their secrets), but how are these inserted into the following line?

request.Credentials = new NetworkCredential(username, password);

Is there concatenation involved? If so can you elaborate? I’ve read up on this version switch a lot, and it’s really surprising there are pretty much no C# examples out there. I also tried something like this:

TwitterResponse Response = TwitterStatus.?
But those methods require a statusId, which I’m pretty sure I don’t have.

Note: Our API key and other stuff was set up a while ago, so we have those values. Also, the request call I’m using looks like this:

Any advice greatly appreciated.


You need to use OAuth in Twitter API v1.1. You can visit the docs to figure out how to write the OAuth code. Alternatively, you can use one of the libraries listed at: https://dev.twitter.com/docs/twitter-libraries

LINQ to Twitter supports OAuth and Twitter API too: https://linqtotwitter.codeplex.com/.


I’m way past this point now, but am having trouble authenticating my program; when attempting to get the response, I get 410: Unauthorized (using regular authentication). With application-only auth I get a 400: Bad Request (I think because I do not have OAuth2.0 - can someone verify and if so, provide me with info on how to get OAuth2.0 on my app?).

Can someone advise what I am doing wrong here?
My code for user authentication (based on https://dev.twitter.com/docs/auth/authorizing-request):

Code updated 06/18

var oauth_version = "1.0"; var oauth_signature_method = "HMAC-SHA1"; var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); var timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString(); var resource_url = requestCall; var screen_name = string.Empty; if (requestCall.Contains("classactionabd")) { screen_name = "classactionabd"; } else screen_name = "project_HEART";
        var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
        var baseString = string.Format(baseFormat,

        baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));
        var compositeKey = string.Concat(Uri.EscapeDataString(consumerSecret), "&", Uri.EscapeDataString(tokenSecret));
        string oauth_signature;
        using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));

        var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                           "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                           "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +

        var authHeader = string.Format(headerFormat,
        // Make the request
        ServicePointManager.Expect100Continue = false;
        var postBody = "screen_name=" + Uri.EscapeDataString(screen_name);//
        resource_url += "?" + postBody;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";

        HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

This keeps getting 410 Unauthorized.

Any help greatly appreciated…


Hate to double post, but it’s been a week since I posted this. Can anyone help me here?


I found a solution: http://stackoverflow.com/questions/17067996/authenticate-and-request-a-users-timeline-with-twitter-api-1-1-oauth

The first answer. Please mark this thread closed. Thanks.


Mtabaana 25,000 thousand did not find them


Hey @ClassActionABD can you share me your code, I need to code app in c#, where app ask for user and pass to login to twitter.

Thanks a lot.


This might a bit late, but I’ve the code sample using OAuth with new API(1.1) here if anybody interested