title: REST API authentication (OAuth 1.0) using C#
body: <p>I'm trying to connect to the Bricklink REST API using OAuth (<a href="http://apidev.bricklink.com/redmine/projects/bricklink-api/wiki/Authorization" rel="noreferrer">http://apidev.bricklink.com/redmine/projects/bricklink-api/wiki/Authorization</a>).</p>
  
  <p>It should be pretty straight forward. However, I'm currently stuck and keep getting a SIGNATURE_INVALID error. My current attempt is shown below. Any suggestions?</p>
  
  <pre><code>        const string consumerKey = "";
          const string consumerSecret = "";
          const string tokenSecret = "";
          const string tokenValue = "";
          const string url = "https://api.bricklink.com/api/store/v1/items/part/3001";
  
          var httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
          httpWebRequest.Method = "GET";
  
          var timeStamp = ((int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
          var nonce = Convert.ToBase64String(Encoding.UTF8.GetBytes(timeStamp));
  
          var signatureBaseString = httpWebRequest.Method.ToUpper() + "&amp;";
          signatureBaseString = signatureBaseString + url.ToLower() + "&amp;";
          signatureBaseString = signatureBaseString + "oauth_consumer_key=" + consumerKey + "&amp;";
          signatureBaseString = signatureBaseString + "oauth_nonce=" + nonce + "&amp;";
          signatureBaseString = signatureBaseString + "oauth_signature_method=" + "HMAC-SHA1" + "&amp;";
          signatureBaseString = signatureBaseString + "oauth_timestamp=" + timeStamp + "&amp;";
          signatureBaseString = signatureBaseString + "oauth_token=" + tokenValue + "&amp;";
          signatureBaseString = signatureBaseString + "oauth_version=" + "1.0";
          signatureBaseString = Uri.EscapeDataString(signatureBaseString);
          Console.WriteLine(signatureBaseString);
  
          var signatureEncoding = new ASCIIEncoding();
          var keyBytes = signatureEncoding.GetBytes(consumerSecret + "&amp;" + tokenSecret);
          var signatureBaseBytes = signatureEncoding.GetBytes(signatureBaseString);
          string signatureString;
          using (var hmacsha1 = new HMACSHA1(keyBytes))
          {
              var hashBytes = hmacsha1.ComputeHash(signatureBaseBytes);
              signatureString = Convert.ToBase64String(hashBytes);
          }
          signatureString = Uri.EscapeDataString(signatureString);
          Console.WriteLine(signatureString);
  
          string SimpleQuote(string x) =&gt; '"' + x + '"';
          var header =
              "OAuth realm=" + SimpleQuote("") + "," +
              "oauth_consumer_key=" + SimpleQuote(consumerKey) + "," +
              "oauth_nonce=" + SimpleQuote(nonce) + "," +
              "oauth_signature_method=" + SimpleQuote("HMAC-SHA1") + "," +
              "oauth_timestamp=" + SimpleQuote(timeStamp) + "," +
              "oauth_token=" + SimpleQuote(tokenValue) + "," +
              "oauth_version=" + SimpleQuote("1.0") + "," +
              "oauth_signature= " + SimpleQuote(signatureString);
          Console.WriteLine(header);
          httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, header);
  
          var response = httpWebRequest.GetResponse();
          var characterSet = ((HttpWebResponse) response).CharacterSet;
          var responseEncoding = characterSet == ""
              ? Encoding.UTF8
              : Encoding.GetEncoding(characterSet ?? "utf-8");
          var responsestream = response.GetResponseStream();
          if (responsestream == null)
          {
              throw new ArgumentNullException(nameof(characterSet));
          }
          using (responsestream)
          {
              var reader = new StreamReader(responsestream, responseEncoding);
              var result = reader.ReadToEnd();
              Console.WriteLine(result);
          }
  </code></pre>
  
  <p>I know that consumerKey, consumerSecret, tokenSecret, and tokenValue are correct as I can connect using the bricklink-api (<a href="https://www.npmjs.com/package/bricklink-api" rel="noreferrer">https://www.npmjs.com/package/bricklink-api</a>) using JavaScript.</p>