Keep getting a 401 Unauthorized when I try to do a POST to get a request token to authenticate with Twitter


#1

I am trying to setup my site so I can log in using twitter and I am unable to get past getting a request token. I keep getting a 401 Unauthorized when I try running this.

Here is a snippet of code that I am using to get the request token. I am using OAuthBase.cs to generate the signature, nonce and timestamp. I am not adding any oauth_token to the header for this request since this is the first hit to get a request token.

Anyone know how I can get this working or what pivotal piece I am missing? I would really appreciate any help!!

            var callbackUrl = HttpUtility.UrlEncode(Consumer.CallbackUrl);
            var consumerKey = Consumer.ConsumerKey;
            var consumerSecret = Consumer.ConsumerSecret;
            var nonce = OAuthUtility.GenerateNonce();

            var timeStamp = OAuthUtility.GenerateTimeStamp();
            

            var normalizedUrl = String.Empty;
            var normalizedRequestParameters = String.Empty;

            var signature = HttpUtility.UrlEncode(OAuthUtility.GenerateSignature(new Uri(ServiceProvider.RequestUrl), consumerKey, consumerSecret, null, null, "POST",
                                   timeStamp, nonce, out normalizedUrl,
                                   out normalizedRequestParameters));

            var builder = new StringBuilder("Authorization: OAuth realm = \"Twitter API\" ");
            builder.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthNonceKey, nonce);
            builder.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthCallbackKey, callbackUrl);
            builder.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthSignatureMethodKey, OAuthBase.HMACSHA1SignatureType);
            builder.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthTimestampKey, timeStamp);
            builder.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthConsumerKeyKey, consumerKey);
            builder.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthSignatureKey, signature);
            builder.AppendFormat("{0}=\"{1}\"", OAuthBase.OAuthVersionKey, OAuthBase.OAuthVersion);

            var header = builder.ToString();

            var confirmed = false;
            var requestToken = String.Empty;

            using (var webClient = new WebClient())
            {
                webClient.Headers.Add(header);
                webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";               
                webClient.Encoding = System.Text.Encoding.UTF8;
                webClient.Headers[HttpRequestHeader.UserAgent] = "Twitterizer/1.0";

                var response = webClient.UploadString(normalizedUrl, "POST", String.Empty);
             
                var itemsDictionary = response.Split('&').Select(item => item.Split('=')).ToDictionary(kvp => kvp[0], kvp => kvp[1]);
                requestToken = itemsDictionary["oauth_token"];
                confirmed =  Convert.ToBoolean(itemsDictionary["oauth_callback_confirmed"]);            
            }