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


#1

Hello,

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:
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=classactionabd&count=60

Any advice greatly appreciated.
Thanks.


#2

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/.


#3

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}" +
                        "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}";
        var baseString = string.Format(baseFormat,
                                    consumerKey,
                                    oauth_nonce,
                                    oauth_signature_method,
                                    oauth_timestamp,
                                    token,
                                    oauth_version,
                                     Uri.EscapeDataString(screen_name)
                                    );

        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}\", " +
                           "oauth_version=\"{6}\"";

        var authHeader = string.Format(headerFormat,
                                Uri.EscapeDataString(oauth_nonce),
                                Uri.EscapeDataString(oauth_signature_method),
                                Uri.EscapeDataString(oauth_timestamp),
                                Uri.EscapeDataString(consumerKey),
                                Uri.EscapeDataString(token),
                                Uri.EscapeDataString(oauth_signature),
                                Uri.EscapeDataString(oauth_version)
                        );
        
        // 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…
-Paul


#4

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


#5

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.


#6

Mtabaana 25,000 thousand did not find them


#7

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.


#8

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