title: Error connecting to azure blob storage API from R
body: <p>I am attempting to work with Azure storage via <a href="https://msdn.microsoft.com/en-us/library/azure/dd179355.aspx" rel="nofollow noreferrer">the REST API</a> in R. I'm using the package <a href="http://github.com/hadley/httr" rel="nofollow noreferrer"><code>httr</code></a> which overlays Curl.</p>
  
  <h2>Setup</h2>
  
  <p>You can use R-fiddle: <a href="http://www.r-fiddle.org/#/fiddle?id=vh8uqGmM" rel="nofollow noreferrer">http://www.r-fiddle.org/#/fiddle?id=vh8uqGmM</a></p>
  
  <pre><code>library(httr)
  requestdate&lt;-format(Sys.time(),"%a, %d %b %Y %H:%M:%S GMT")
  url&lt;-"https://preconstuff.blob.core.windows.net/pings?restype=container&amp;comp=list"
  sak&lt;-"Q8HvUVJLBJK+wkrIEG6LlsfFo19iDjneTwJxX/KXSnUCtTjgyyhYnH/5azeqa1bluGD94EcPcSRyBy2W2A/fHQ=="
  signaturestring&lt;-paste0("GET",paste(rep("\n",12),collapse=""),
  "x-ms-date:",requestdate,"
  x-ms-version:2009-09-19
  /preconstuff/pings
  comp:list
  restype:container")
  
  headerstuff&lt;-add_headers(Authorization=paste0("SharedKey preconstuff:",
                           RCurl::base64(digest::hmac(key=sak,
                           object=enc2utf8(signaturestring),
                           algo= "sha256"))),
                      `x-ms-date`=requestdate,
                      `x-ms-version`= "2009-09-19")
  </code></pre>
  
  <p>Trying to list blobs:</p>
  
  <pre><code>content(GET(url,config = headerstuff, verbose() ))
  </code></pre>
  
  <h2>Error</h2>
  
  <h3>Top level message</h3>
  
  <blockquote>
    <p>The MAC signature found in the HTTP request 'Q8HvUVJLBJK+wkrIEG6LlsfFo19iDjneTwJxX/KXSnUCtTjgyyhYnH/5azeqa1bluGD94EcPcSRyBy2W2A/fHQ==' is not the same as any computed signature.</p>
  </blockquote>
  
  <h3>Response content</h3>
  
  <pre><code>[1] "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;&lt;Error&gt;
  &lt;Code&gt;AuthenticationFailed&lt;/Code&gt;&lt;Message&gt;Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:1ab26da5-0001-00dc-6ddb-15e35c000000\nTime:2015-03-26T17:51:42.7190620Z&lt;/Message&gt;
  &lt;AuthenticationErrorDetail&gt;The MAC signature found in the HTTP request 'NTM1ODZjMjhhZmMyZGM3NDM0YTFjZDgwNGE0ODVmMzVjNDhkNjBkNzk1ZjNkZjJjOTNlNjUxYTMwMjRhNzNlYw==' is not the same as any computed signature. Server used following string to sign: 
  'GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:Thu, 26 Mar 2015 17:52:37 GMT\nx-ms-version:2009-09-19\n/preconstuff/pings\ncomp:list\nrestype:container'.
  &lt;/AuthenticationErrorDetail&gt;&lt;/Error&gt;"
  </code></pre>
  
  <h3>Verbose output</h3>
  
  <pre><code>-&gt; GET /pings?restype=container&amp;comp=list HTTP/1.1
  -&gt; User-Agent: curl/7.39.0 Rcurl/1.95.4.5 httr/0.6.1
  -&gt; Host: preconstuff.blob.core.windows.net
  -&gt; Accept-Encoding: gzip
  -&gt; Accept: application/json, text/xml, application/xml, */*
  -&gt; Authorization: SharedKey preconstuff:OTRhNTgzYmY3OTY3M2UzNjk3ODdjMzk3OWM3ZmU0OTA4MWU5NTE2OGYyZGU3YzRjNjQ1M2NkNzY0ZTcyZDRhYQ==
  -&gt; x-ms-date: Thu, 26 Mar 2015 17:56:27 GMT
  -&gt; x-ms-version: 2009-09-19
  -&gt; 
  &lt;- HTTP/1.1 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
  &lt;- Content-Length: 719
  &lt;- Content-Type: application/xml
  &lt;- Server: Microsoft-HTTPAPI/2.0
  &lt;- x-ms-request-id: 3d47770c-0001-0085-2313-6d466f000000
  &lt;- Date: Thu, 26 Mar 2015 17:56:27 GMT
  &lt;- 
  </code></pre>
  
  <h2>Resolving the error</h2>
  
  <p>Googling for this issue doesn't seem to yield a consistent cause, but it's likely due to bad formatting / request structure on my part. To that end I checked:</p>
  
  <ol>
  <li>I've verified my key is correct (it's just c&amp;p from portal)</li>
  <li>I've made sure the date is correctly formatted</li>
  <li>There was a <a href="https://stackoverflow.com/questions/28723398/azure-documentdb-the-mac-signature-found-in-the-http-request-is-not-the-same-a">recent documentDB SO</a> that suggested it could be to  be a clock skew issue and I do note that my x-ms-date is a second ahead of the Date in the response. I've tried sending a fixed value that was definitely in the past, but within the 15 mins tolerance. Didn't get a change in message.</li>
  <li><strike> Added <code>encoding="Base64"</code> in <code>headerstuff</code> further to an <a href="https://social.msdn.microsoft.com/forums/azure/en-US/dee2eaf7-59b6-4b4f-b68c-f0b4dacbb867/signature-is-not-the-same-as-any-computed-signature" rel="nofollow noreferrer">MSDN forum question</a> but the same error message was returned</strike></li>
  <li>Further to @Serdar's answer, I incorporated the construction of a signature string (I've verified that this matches the one provided in the error message so it), then encoding in base64 a hmac-sha256 (using the secondary access key (<code>sak</code>) as the encryption key) version of the UTF8 converted <code>signaturestring</code> as the value to be used in the SharedKey authorisation.</li>
  <li>Further to @Serdar's comment, the date used in the signature string and for the main request must be the same, so defined once and reused</li>
  </ol>
  
  <p><strong>Is there anything obviously wrong? Are there other things to check? Does the code work for others?</strong></p>