title: C# Request Token 401 Unauthorized access
body: <p>I am trying to generate a <code>request_token</code> using the oAuth method as mentioned in the links below:</p>
  
  <p><a href="https://dev.twitter.com/oauth/overview/creating-signatures" rel="nofollow">https://dev.twitter.com/oauth/overview/creating-signatures</a>
  <a href="https://dev.twitter.com/oauth/reference/post/oauth/request_token" rel="nofollow">https://dev.twitter.com/oauth/reference/post/oauth/request_token</a></p>
  
  <p>No matter how many times I go through the Twitter API oauth procedure, am failing to make my request work.</p>
  
  <p>I came across this blog with an example on how to go about updating a user timeline. </p>
  
  <p><a href="https://garyshortblog.wordpress.com/2011/02/11/a-twitter-oauth-example-in-c/" rel="nofollow">https://garyshortblog.wordpress.com/2011/02/11/a-twitter-oauth-example-in-c/</a></p>
  
  <p>Using the same concept I modified it a little to request a token</p>
  
  <pre><code>public string signedSignature(string status, string postBody, string oauth_consumer_key, string oauth_nonce, string oauth_signature_method,
                                      string oauth_token, string callbackURL, string oauth_timestamp, string oauth_version)
      {            
  
          //GS - When building the signature string the params
          //must be in alphabetical order. I can't be bothered
          //with that, get SortedDictionary to do it's thing
          SortedDictionary&lt;string, string&gt; sd =
              new SortedDictionary&lt;string, string&gt;();
  
          //sd.Add("status", status);
          sd.Add("include_entities", "true");
          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_token", oauth_token);
          sd.Add("oauth_version", oauth_version);
  
          //GS - Build the signature string
          string baseString = String.Empty;
          baseString += "POST" + "&amp;";
          baseString += Uri.EscapeDataString(
              "https://api.twitter.com/oauth/request_token")
              + "&amp;";
  
          foreach (KeyValuePair&lt;string, string&gt; entry in sd)
          {
              baseString += Uri.EscapeDataString(entry.Key +
                  "=" + entry.Value + "&amp;");
          }
  
          //GS - Remove the trailing ambersand char, remember 
          //it's been urlEncoded so you have to remove the 
          //last 3 chars - %26
          baseString =
              baseString.Substring(0, baseString.Length - 3);
  
          //GS - Build the signing key
          string consumerSecret =
              "&lt;consumer Secret&gt;";
  
          string oauth_token_secret =
              "&lt;token secret&gt;";
  
          string signingKey =
              Uri.EscapeDataString(consumerSecret) + "&amp;" +
              Uri.EscapeDataString(oauth_token_secret);
  
          //GS - Sign the request
          HMACSHA1 hasher = new HMACSHA1(
              new ASCIIEncoding().GetBytes(signingKey));
  
          string signatureString = Convert.ToBase64String(
              hasher.ComputeHash(
              new ASCIIEncoding().GetBytes(baseString)));
  
          return signatureString;
      }
  
  public ActionResult AccessToken()
      {            
          //GS - Get the oAuth params
          string status = "Hello Ladies + Gentlemen, a signed OAuth request!";
          string postBody = "status=" +
              Uri.EscapeDataString(status);
  
          string oauth_consumer_key = "bidjtABOkF0b3mvw1UaHWDf7x";
          string oauth_nonce = Convert.ToBase64String(
              new ASCIIEncoding().GetBytes(
                  DateTime.Now.Ticks.ToString()));
  
          string oauth_signature_method = "HMAC-SHA1";
          string oauth_token =
              "84473240-brz5BNw9r2WfbufzJ2WjaLysCBHmJjhjJxMVGz8Od";
  
          string callbackURL = Uri.EscapeDataString("http://localhost:37808/");
  
          TimeSpan ts = DateTime.UtcNow -
              new DateTime(1970, 1, 1, 0, 0, 0, 0);
  
          string oauth_timestamp =
              Convert.ToInt64(ts.TotalSeconds).ToString();
  
          string oauth_version = "1.0";
  
          string sSig = signedSignature(status, postBody, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_token, 
                                      callbackURL, oauth_timestamp, oauth_version);
  
          //GS - Tell Twitter we don't do the 100 continue thing
          ServicePointManager.Expect100Continue = false;
  
          //GS - Instantiate a web request and populate the 
          //authorization header
          HttpWebRequest hwr =
              (HttpWebRequest)WebRequest.Create(
              @"https://api.twitter.com/oauth/request_token");
  
          string authorizationHeaderParams = String.Empty;
          authorizationHeaderParams += "OAuth ";
          authorizationHeaderParams += "oauth_nonce=" + "\"" +
              Uri.EscapeDataString(oauth_nonce) + "\",";
  
          authorizationHeaderParams +=
              "oauth_signature_method=" + "\"" +
              Uri.EscapeDataString(oauth_signature_method) +
              "\",";
          authorizationHeaderParams += "oauth_callback=" + "\"" +
              callbackURL + "\",";
  
          authorizationHeaderParams += "oauth_timestamp=" + "\"" +
              Uri.EscapeDataString(oauth_timestamp) + "\",";
  
          authorizationHeaderParams += "oauth_consumer_key="
              + "\"" + Uri.EscapeDataString(
              oauth_consumer_key) + "\",";
  
          authorizationHeaderParams += "oauth_signature=" + "\""
              + Uri.EscapeDataString(sSig) + "\",";
  
          authorizationHeaderParams += "oauth_version=" + "\"" +
              Uri.EscapeDataString(oauth_version) + "\"";
  
          hwr.Headers.Add(
              "Authorization", authorizationHeaderParams);
  
          //GS - POST off the request
          hwr.Method = "POST";
          hwr.ContentType = "application/x-www-form-urlencoded";
          Stream stream = hwr.GetRequestStream();
          byte[] bodyBytes =
              new ASCIIEncoding().GetBytes(postBody);
  
          stream.Write(bodyBytes, 0, bodyBytes.Length);
          stream.Flush();
          stream.Close();
  
          //GS - Allow us a reasonable timeout in case
          //Twitter's busy
          hwr.Timeout = 3 * 60 * 1000;
  
          try
          {
                  HttpWebResponse rsp = hwr.GetResponse()
                      as HttpWebResponse;
              rsp.StatusCode.ToString();
                  //GS - Do something with the return here...
          }
          catch (WebException e)
          {
              //GS - Do some clever error handling here...
          }
  
          return View();            
      }
  </code></pre>
  
  <p>I am not sure where exactly am going wrong. I have gone through the documents several times and cant seem to find out where I have gone wrong?</p>
  
  <p>The error I get is <code>401 Unauthorized access</code></p>