title: Amazon S3 REST API 403 error c#
body: <p>I'm trying to get some code working to fetch a file from S3 using the REST API via C#. I've seen other people doing similar things but for some reason I keep getting a 403 error. I've tried to do the same thing with the AWS SDK for .Net and it works so I assume it's the way I'm creating the authorization header.</p>
  
  <p>Is anyone able to shed any light on this please?</p>
  
  <pre><code>string awsAccessId = "***";
  string awsSecretKey = "***";
  string bucketName = "thebucket";
  
  string httpDate = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss +0000\n");
                  string canonicalString = "GET\n"
                                          + "\n"
                                          + "\n"
                                          + "x-amz-date:" + httpDate + "\n"
                                          + "/" + bucketName + "/readme.txt";
  
                  // now encode the canonical string
                  Encoding ae = new UTF8Encoding();
                  // create a hashing object
                  HMACSHA1 signature = new HMACSHA1();
                  // secretId is the hash key
                  signature.Key = ae.GetBytes(awsSecretKey);
                  byte[] bytes = ae.GetBytes(canonicalString);
                  byte[] moreBytes = signature.ComputeHash(bytes);
                  // convert the hash byte array into a base64 encoding
                  string encodedCanonical = Convert.ToBase64String(moreBytes);
  
                  // Send the request
                  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://" + bucketName +".s3.amazonaws.com/readme.txt");
                  request.Headers.Add("x-amz-date", httpDate);
                  request.Headers.Add("Authorization", "AWS " + awsAccessId + ":" + encodedCanonical);
                  request.Method = "GET";
  
                  // Get the response
                  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                  Console.WriteLine(response.StatusCode);
                  Console.Read();
  </code></pre>