title: Twitter get request_token problems with async - 401 Error
body: <p>I'm having problems getting a request token back from Twitter asychronously - the server returns "The remote server returned an error: (401) Unauthorized."</p>
  
  <p>Here's the code I'm using -</p>
  
  <pre><code>public void AcquireRequestToken(Action&lt;bool&gt; response)
  {
  
      string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
  
      // build the signature
      var headers = new Dictionary&lt;string,string&gt;()
      {
          { "oauth_consumer_key", _oAuthConfig.ConsumerKey },
          { "oauth_nonce", oauth_nonce },
          { "oauth_signature_method", "HMAC-SHA1" },
          { "oauth_timestamp", MakeTimestamp() },
          { "oauth_version", "1.0" },
          { "oauth_callback", PercentEncode(_oAuthConfig.Callback) },
      };
  
      string signature = MakeSignature ("POST", _oAuthConfig.RequestTokenUrl, headers);
      string compositeSigningKey = MakeSigningKey(_oAuthConfig.ConsumerSecret, null);
      string oauth_signature = MakeOAuthSignature(compositeSigningKey, signature);
  
      Uri fullUri = new Uri(_oAuthConfig.RequestTokenUrl);
  
      var request = (HttpWebRequest)WebRequest.Create(fullUri);
      request.Method = "POST";
  
      request.Headers.Add("oauth_consumer_key", PercentEncode(_oAuthConfig.ConsumerKey));
      request.Headers.Add("oauth_nonce", PercentEncode(oauth_nonce));
      request.Headers.Add("oauth_signature_method", PercentEncode("HMAC-SHA1"));
      request.Headers.Add("oauth_timestamp", PercentEncode(MakeTimestamp()));
      request.Headers.Add("oauth_version", "1.0");
      request.Headers.Add("oauth_callback", PercentEncode(_oAuthConfig.Callback));
      request.Headers.Add("oauth_signature", PercentEncode(oauth_signature));
  
      try 
      {
  
          request.BeginGetResponse(new AsyncCallback(result =&gt;
          {
              string contents = String.Empty;
              HttpWebRequest theRequest = (HttpWebRequest)result.AsyncState;
  
              if (theRequest != null)
              {
                  try
                  {
                      HttpWebResponse theResponse = (HttpWebResponse)theRequest.EndGetResponse(result);
  
                      using (Stream stream = theResponse.GetResponseStream())
                      using (StreamReader reader = new StreamReader(stream))
                      {
                          contents = reader.ReadToEnd();
                      }
  
                      Dictionary&lt;string, object&gt; results = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(contents);
  
                      _requestToken = (string)results ["oauth_token"];
                      _requestTokenSecret = (string)results ["oauth_token_secret"];
  
                      response(true);
                  }
                  catch (WebException e)
                  {
                      response(false);
                  }
              }
              else
              {
                  response(false);
              }
          }), request);
  
      }
      catch (WebException e)
      {
          response(false);
      }
  
  }
  </code></pre>
  
  <p>My old (non-async) code works fine -</p>
  
  <pre><code>public bool AcquireRequestToken()
  {
      var headers = new Dictionary&lt;string,string&gt;()
      {
          { "oauth_callback", PercentEncode(_oAuthConfig.Callback) },
          { "oauth_consumer_key", _oAuthConfig.ConsumerKey },
          { "oauth_signature_method", "HMAC-SHA1" },
          { "oauth_timestamp", MakeTimestamp() },
          { "oauth_version", "1.0" }
      };
  
      string signature = MakeSignature ("POST", _oAuthConfig.RequestTokenUrl, headers);
      string compositeSigningKey = MakeSigningKey(_oAuthConfig.ConsumerSecret, null);
      string oauth_signature = MakeOAuthSignature(compositeSigningKey, signature);
  
      var wc = new WebClient ();
      headers.Add ("oauth_signature", PercentEncode(oauth_signature));
      wc.Headers [HttpRequestHeader.Authorization] = HeadersToOAuth(headers);
  
      try
      {
          var result = HttpUtility.ParseQueryString(wc.UploadString (new Uri(_oAuthConfig.RequestTokenUrl), ""));
  
          if (result ["oauth_callback_confirmed"] != null)
          {
              _requestToken = result ["oauth_token"];
              _requestTokenSecret = result ["oauth_token_secret"];
  
              return true;
          }
      }
      catch (Exception e)
      {
          return false;
      }
  
  }
  </code></pre>
  
  <p>The headers are slightly different in the new version, but keeping them the same doesn't help.</p>
  
  <p>Thanks for any suggestions!</p>