title: Calling API: How to provide these parameters (key, nonce and signature)
body: <p>I am playing around implementing an API. Usually that is really simple, but this one gives me problems. <strong>However, I am pretty sure the problem is me</strong>, and not the API.</p>
  
  <p>Url to this specific API:</p>
  
  <p><a href="https://www.bitstamp.net/api/" rel="noreferrer">https://www.bitstamp.net/api/</a></p>
  
  <p>I want to make  POST call to the "Account balance". Currently I get the following answer:</p>
  
  <pre><code>{"error": "Missing key, signature and nonce parameters"}
  </code></pre>
  
  <p>and I try to do it with the following code:</p>
  
  <pre><code>        var path = "https://www.bitstamp.net/api/user_transactions";
          var nonce = GetNonce();
          var signature = GetSignature(nonce);
  
          using (WebClient client = new WebClient())
          {
  
              byte[] response = client.UploadValues(path, new NameValueCollection()
             {
                 { "key", Constants.ThirdParty.BitStamp.ApiKey },
                 { "signature", signature },
                 { "nonce", nonce},
  
             });
              var str = System.Text.Encoding.Default.GetString(response);
          }
  
          return 0.0m;
  </code></pre>
  
  <p>This is my two helper functions:</p>
  
  <pre><code>    private string GetSignature(int nonce)
      {
          string msg = string.Format("{0}{1}{2}", nonce,
              Constants.ThirdParty.BitStamp.ClientId,
              Constants.ThirdParty.BitStamp.ApiKey);
  
          return HelperFunctions.sign(Constants.ThirdParty.BitStamp.ApiSecret, msg);
      }
  
      public static int GetNonce()
      {
          return (int) (DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds;
      }
  </code></pre>
  
  <p>My crypto sign function is this one:</p>
  
  <pre><code>    public static String sign(String key, String stringToSign)
      {
          System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  
          byte[] keyByte = encoding.GetBytes(key);
          HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);
  
          return Convert.ToBase64String(hmacsha256.ComputeHash(encoding.GetBytes(stringToSign)));
      }
  </code></pre>
  
  <p>Any idea why i get the "missing key" error? Is there something obvious I am doing wrong (probably is :) )? </p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>Fiddler tells me I post the following data:</p>
  
  <pre><code>key=mykeymykey&amp;signature=PwhdkSek6GP%2br%2bdd%2bS5aU1MryXgrfOD4fLH05D7%2fRLQ%3d&amp;nonce=1382299103%2c21055
  </code></pre>
  
  <p><strong>Edit #2</strong>:</p>
  
  <p>Updated code on generating the signature:</p>
  
  <pre><code>private string GetSignature(int nonce)
      {
          string msg = string.Format("{0}{1}{2}", nonce,
              Constants.ThirdParty.BitStamp.ClientId,
              Constants.ThirdParty.BitStamp.ApiKey);
  
          return HelperFunctions.ByteArrayToString(HelperFunctions.SignHMACSHA256(
              Constants.ThirdParty.BitStamp.ApiSecret, msg)).ToUpper();
      }
  public static byte[] SignHMACSHA256(String key, byte[] data)
  {
      HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
      return hashMaker.ComputeHash(data);
  }
  
  public static byte[] StrinToByteArray(string str)
  {
      byte[] bytes = new byte[str.Length * sizeof(char)];
      System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
      return bytes;
  }
  
  public static string ByteArrayToString(byte[] ba)
  {
      return BitConverter.ToString(ba).Replace("-", "");
  }
  </code></pre>