title: Azure - call Storage rest api for list blobs
body: <p>What I am trying to do is connect to the Azure Storage Rest API List Blobs. Ref: <a href="http://msdn.microsoft.com/en-us/library/windowsazure/dd135734.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windowsazure/dd135734.aspx</a></p>
  
  <p>I have tried to follow <a href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx</a> in order to specify the authorization header however I get a 403 error - forbidden.</p>
  
  <p>Code:</p>
  
  <pre><code>Uri address = new Uri("https://account.blob.core.windows.net/$logs?restype=container&amp;comp=list");
  HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
  req.Headers["x-ms-date"] = "2013-09-04";
  req.Headers["x-ms-version"] = "2012-02-12";
  req.Method = "GET";
  
  string StringToSign =  "GET\n"
      + "\n" // content encoding
      + "\n" // content language
      + "\n" // content length
      + "\n" // content md5
      + "\n" // content type
      + "\n" // date
      + "\n" // if modified since
      + "\n" // if match
      + "\n" // if none match
      + "\n" // if unmodified since
      + "\n" // range
      + "x-ms-date: 2013-09-04\nx-ms-version:2012-02-12\n" // headers
      + "/account/blob\ncomp:list\nrestype:container"; // resources
  
  string accountName = "account";
  string key = Convert.ToBase64String(Encoding.Default.GetBytes(StringToSign));
  req.Headers["Authorization"] = string.Format("SharedKey {0}:{1}", accountName, key);
  
  HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
  </code></pre>
  
  <p>Can anyone see any mistakes? Is there a tool which can generate the key? One thing I am not sure of is I am encoding/hashing the string correctly.</p>
  
  <p>Thanks,
  Andrew</p>
  
  <p>Update with latest code. This code gives me a Forbidden error.</p>
  
  <pre><code>DateTime dt = DateTime.UtcNow;
  string StringToSign = "GET\n"
      + "\n" // content encoding
      + "\n" // content language
      + "\n" // content length
      + "\n" // content md5
      + "\n" // content type
      + "\n" // date
      + "\n" // if modified since
      + "\n" // if match
      + "\n" // if none match
      + "\n" // if unmodified since
      + "\n" // range
      + "x-ms-date: " + dt.ToString("R") + "\nx-ms-version:2012-02-12\n" // headers
      + "/account/$logs\ncomp:list\nrestype:container";
  
  string auth = SignThis(StringToSign, "accountkey", "account");
  string method = "GET";
  string urlPath = "https://account.blob.core.windows.net/$logs?restype=container&amp;comp=list";
  Uri uri = new Uri(urlPath);
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  request.Method = method;
  request.Headers.Add("x-ms-date", dt.ToString("R"));
  request.Headers.Add("x-ms-version", "2012-02-12");
  request.Headers.Add("Authorization", auth);
  
  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  {
  }
  </code></pre>