401 authorization error, signature string verified


#1

Hi I’m trying to authenticate via Oauth using C#. I am receiving a 401 Authorization Error. I’ve used the oauth tester at http://quonos.nl/oauthTester/ and reviewed the sample query from twitter’s Oauth tool and the queries are similar. All the relevant info is below. Can you offer any advice?

I’m using the HttpWebRequest and HttpWebResponse Class for making the initial POST request.

Using URL: https://api.twitter.com/1.1/oauth/request_token

Signature Base String:
POST&https%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&oauth_consumer_key%3Dfbpvyf2acxvtc8axbsbgGssDK%26oauth_nonce%3DNjM1MzkyOTc4NDY5MzMyMTE0%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1403719046%26oauth_version%3D1.0

My signature string:
OaRUed1QEDkanrZ7Z+tWfOp9TlY=

My Authorization header is the following:
Oauth oauth_consumer_key=“fbpvyf2acxvtc8axbsbgGssDK”, oauth_nonce=“NjM1MzkyOTc1NDg2ODcyMTE0”, oauth_signature=“OaRUed1QEDkanrZ7Z%2BtWfOp9TlY%3D”, oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1403718748”, oauth_version=“1.0”, oauth_callback=“oob”

            Uri test = new Uri("https://api.twitter.com/1.1/oauth/request_token");

            HttpWebRequest TwRequest = (HttpWebRequest)WebRequest.Create(test);
            HttpWebResponse TwResponse;

            TwRequest.Method = "POST";
            TwRequest.ContentType = "application/x-www-form-urlencoded";

            TwitterApp MyApp = new TwitterApp();
            Authorize RestAuthorize = new Authorize();

            string auth_header = RestAuthorize.GetAuthorizationHeader(MyApp);
            auth_header =  auth_header+", " + "oauth_callback=" + "\"" + "oob" +"\"" ;
            auth_header = "Oauth "+ auth_header;
            System.IO.File.WriteAllText(@"C:\Users\stefan.dye\Desktop\REST_auth.txt", auth_header);

            TwRequest.Headers.Add("Authorization", auth_header);
            byte[] post_data = System.Text.Encoding.ASCII.GetBytes(auth_header);
           
            
           // TwRequest.ContentLength = post_data.Length;
           // string[] my_header = auth_header.Split(',');

            //StringBuilder new_header = new StringBuilder();
          //  new_header.Append(my_header[0]);

            StreamWriter writer = new StreamWriter(TwRequest.GetRequestStream());
            writer.Write("");
      

            
            Console.WriteLine(TwRequest.Headers.ToString());
            
            //Stream newStream = TwRequest.GetRequestStream();

            //newStream.Write(post_data, 0, post_data.Length);
            //newStream.Close();
            writer.Close();
            try
            {
                TwResponse = (HttpWebResponse)TwRequest.GetResponse();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }