[C#] Authenticating a GET call using OAuth v1.0A


#1

I’m trying to pull the top XX tweets from a user account that I own using GET status/user_timeline, and it seems I need to authenticate the HTTP request to do this. I’ve looked at both user authentication and application-only authentication and can’t get either to work. Getting the response fails. With user authentication I get a 410: Unauthorized. 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?).

My code for user authentication (based on https://dev.twitter.com/docs/auth/authorizing-request):

            // Create nonce...
            string oauthNonce = Convert.ToBase64String(Encoding.UTF8.GetBytes("3kj43lj3hdr3ijr9erj34j74j42j56k0"));
            //Create signature
            List<string> arrayofParms = AddParametersToArray(consumerKey, oauthNonce, token);
            string parameterString = string.Empty;
            for (int i = 0; i <= 13; i++)
            {
                parameterString += arrayofParms[i];
                if (i % 2 == 0)
                {
                    parameterString += "=";
                }
                else
                {
                    if (i != 13)
                    {
                        parameterString += "&";
                    }
                }
            }

            string signatureBaseString = string.Format("{0}&{1}&{2}", "GET", HttpUtility.UrlEncode(requestCall), HttpUtility.UrlEncode(parameterString));
            string signingKey = string.Format("{0}&{1}", HttpUtility.UrlEncode(consumerSecret), HttpUtility.UrlEncode(tokenSecret));
            HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
            string oauthSignature = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(signatureBaseString)));

            // Build the header string
            List<string> parametersForHeader = arrayofParms;
            parametersForHeader.RemoveAt(0);
            parametersForHeader.RemoveAt(0);
            parametersForHeader.Insert(4, HttpUtility.UrlEncode("oauth_signature"));
            parametersForHeader.Insert(5, HttpUtility.UrlEncode(oauthSignature));
            string finalHeaderString = "OAuth ";
            for (int i = 0; i <= 13; i++)
            {
                finalHeaderString += arrayofParms[i];
                if (i % 2 == 0)
                {
                    finalHeaderString += @"=""";
                }
                else
                {
                    if (i != 13)
                    {
                        finalHeaderString += @""", ";
                    }
                    else finalHeaderString += @"""";
                } 
            }
           
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(requestCall);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers[HttpRequestHeader.Authorization] = finalHeaderString;
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

This keeps getting 410 Unauthorized.

Any help greatly appreciated…
-Paul