【发布时间】:2015-05-29 20:06:24
【问题描述】:
我正在尝试通过 R 中的 the REST API 使用 Azure 存储。我正在使用覆盖 Curl 的包 httr。
设置
您可以使用 R-fiddle:http://www.r-fiddle.org/#/fiddle?id=vh8uqGmM
library(httr)
requestdate<-format(Sys.time(),"%a, %d %b %Y %H:%M:%S GMT")
url<-"https://preconstuff.blob.core.windows.net/pings?restype=container&comp=list"
sak<-"Q8HvUVJLBJK+wkrIEG6LlsfFo19iDjneTwJxX/KXSnUCtTjgyyhYnH/5azeqa1bluGD94EcPcSRyBy2W2A/fHQ=="
signaturestring<-paste0("GET",paste(rep("\n",12),collapse=""),
"x-ms-date:",requestdate,"
x-ms-version:2009-09-19
/preconstuff/pings
comp:list
restype:container")
headerstuff<-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")
尝试列出 blob:
content(GET(url,config = headerstuff, verbose() ))
错误
顶级消息
在 HTTP 请求“Q8HvUVJLBJK+wkrIEG6LlsfFo19iDjneTwJxX/KXSnUCtTjgyyhYnH/5azeqa1bluGD94EcPcSRyBy2W2A/fHQ==”中找到的 MAC 签名与任何计算的签名不同。
回复内容
[1] "<?xml version=\"1.0\" encoding=\"utf-8\"?><Error>
<Code>AuthenticationFailed</Code><Message>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</Message>
<AuthenticationErrorDetail>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'.
</AuthenticationErrorDetail></Error>"
详细输出
-> GET /pings?restype=container&comp=list HTTP/1.1
-> User-Agent: curl/7.39.0 Rcurl/1.95.4.5 httr/0.6.1
-> Host: preconstuff.blob.core.windows.net
-> Accept-Encoding: gzip
-> Accept: application/json, text/xml, application/xml, */*
-> Authorization: SharedKey preconstuff:OTRhNTgzYmY3OTY3M2UzNjk3ODdjMzk3OWM3ZmU0OTA4MWU5NTE2OGYyZGU3YzRjNjQ1M2NkNzY0ZTcyZDRhYQ==
-> x-ms-date: Thu, 26 Mar 2015 17:56:27 GMT
-> x-ms-version: 2009-09-19
->
<- HTTP/1.1 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
<- Content-Length: 719
<- Content-Type: application/xml
<- Server: Microsoft-HTTPAPI/2.0
<- x-ms-request-id: 3d47770c-0001-0085-2313-6d466f000000
<- Date: Thu, 26 Mar 2015 17:56:27 GMT
<-
解决错误
谷歌搜索这个问题似乎并没有产生一致的原因,但这可能是由于我的格式/请求结构不正确。为此,我检查了:
- 我已验证我的密钥是正确的(它只是来自门户的 c&p)
- 我已确保日期格式正确
- 有一个recent documentDB SO 表明它可能是时钟偏差问题,我确实注意到我的 x-ms-date 比响应中的 Date 早一秒。我试过发送一个过去肯定存在的固定值,但在 15 分钟的容差范围内。未收到消息更改。
在headerstuff中添加了encoding="Base64"到MSDN forum question,但返回了相同的错误消息- 在@Serdar 的回答的基础上,我合并了签名字符串的构造(我已经验证这与错误消息中提供的字符串匹配),然后在 base64 中编码一个 hmac-sha256(使用辅助访问密钥(
sak) 作为加密密钥)UTF8 版本转换为signaturestring作为共享密钥授权中使用的值。 - 除@Serdar 的评论外,签名字符串中使用的日期和主请求中使用的日期必须相同,因此定义一次并重复使用
有什么明显的错误吗?还有其他需要检查的吗?该代码对其他人有用吗?
【问题讨论】:
标签: r azure-storage httr