Hello guyz,

I am trying call oauth/request_token in order to oauth_token and oauth_token_secret. First here's the code

            string url = "https://api.twitter.com/oauth/request_token";
            SortedDictionary<string,string> sd = new SortedDictionary<string,string>();
            sd.Add("oauth_callback",oauth_callback);
            sd.Add("oauth_consumer_key",oauth_consumer_key);
            sd.Add("oauth_nonce",oauth_nonce);
            sd.Add("oauth_signature_method",oauth_signature_method);
            sd.Add("oauth_timestamp", oauth_timestamp);
            sd.Add("oauth_version",oauth_version);

            string base_string = String.Empty;
            base_string += "POST" + "&";
            base_string += Uri.EscapeDataString("https://api.twitter.com/oauth/request_token")+"&";

            foreach (KeyValuePair<string,string> entry in sd)
            {
                base_string += Uri.EscapeDataString(entry.Key+"="+entry.Value+"&");
            }

            base_string = base_string.Substring(0,base_string.Length-3);

            var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                        "&");

            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(base_string)));
            }
            string header_format = "OAuth oauth_nonce=\"{0}\", oauth_callback=\"{1}\", oauth_signature_method=\"{2}\", oauth_timestamp=\"{3}\", oauth_consumer_key=\"{4}\", oauth_signature=\"{5}\", oauth_version=\"{6}\"";
            string header = string.Format(header_format, Uri.EscapeDataString(oauth_nonce), Uri.EscapeDataString(oauth_callback), Uri.EscapeDataString(oauth_signature_method), Uri.EscapeDataString(oauth_timestamp), Uri.EscapeDataString(oauth_consumer_key), Uri.EscapeDataString(oauth_signature), Uri.EscapeDataString(oauth_version));

            ServicePointManager.Expect100Continue = false;

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Headers.Add("Authorization", header);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";

            WebResponse response = req.GetResponse();

Now the problem is that the twitter is returning a 401. I don't see what's wrong with my code. The params are in order, i am generating a proper signature.

Please Urgent help is required on this matter.

Thank you.

I'd just use one of the many twitter C# libraries (like this one). You can also use that to see how they accomplished what you are trying to do.

guyz really need help on this one.

There is a reason people have created (several) third party libraries for this. The out-of-the-box way of doing is very long and/or complicated.

Are you using 1.0 or 2.0?

This article has been dead for over six months. Start a new discussion instead.