使用 R 访问 DocumentDB web API
DocumentDB web API access with R
我在尝试使用 R 和 PostMan 连接到 documentDB web API 时遇到以下问题。
在 DocumentDB 文档中,向网络提问的方法 API 是使用 base64 哈希组合授权 header。
在 R 中,我正在尝试计算签名并直接与邮递员一起测试 header。
但我每次都会收到 http 401。
这是我的 R 代码:
toHash <- enc2utf8("get\ncolls\ndbs/toto/colls/testtoto\nsun, 08 may 2016 06:43:05 gmt\n\n")
hash <- hmac(key, toHash, "sha256")
base64(hash)
"key" 是从门户网站获得的主键。
然后,根据 Azure 文档,我的 header 是:
type=master&ver=1.0&sig=< thebase64(hash) >
我正在使用 headers x-ms-version、日期和 x-ms-date.
将其粘贴到 PostMan 中
但是它不起作用..
我现在卡住了,有人有想法吗?我使用了错误的 R 函数吗?一个错误的钥匙,有没有办法获得更多关于不匹配的信息?
网络 api 响应是:
{
"code": "Unauthorized",
"message": "The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'get\ncolls\ndbs/toto/colls/testtoto\nsun, 08 may 2016 06:43:05 gmt\n\n'\r\nActivityId: fadbfc0b-e298-418a-b56c-8114699fff91"
}
我自己发现了问题所在。
Azure 门户中提供的令牌是 base64 编码的。所以必须解码它:
RCurl::base64Decode(key, mode="raw")
以便与 digest::hmac
函数一起使用。在此 hmac
函数中也必须指定 raw = TRUE
。
我在尝试使用 R 和 PostMan 连接到 documentDB web API 时遇到以下问题。
在 DocumentDB 文档中,向网络提问的方法 API 是使用 base64 哈希组合授权 header。
在 R 中,我正在尝试计算签名并直接与邮递员一起测试 header。 但我每次都会收到 http 401。 这是我的 R 代码:
toHash <- enc2utf8("get\ncolls\ndbs/toto/colls/testtoto\nsun, 08 may 2016 06:43:05 gmt\n\n")
hash <- hmac(key, toHash, "sha256")
base64(hash)
"key" 是从门户网站获得的主键。 然后,根据 Azure 文档,我的 header 是:
type=master&ver=1.0&sig=< thebase64(hash) >
我正在使用 headers x-ms-version、日期和 x-ms-date.
将其粘贴到 PostMan 中但是它不起作用..
我现在卡住了,有人有想法吗?我使用了错误的 R 函数吗?一个错误的钥匙,有没有办法获得更多关于不匹配的信息?
网络 api 响应是:
{
"code": "Unauthorized",
"message": "The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'get\ncolls\ndbs/toto/colls/testtoto\nsun, 08 may 2016 06:43:05 gmt\n\n'\r\nActivityId: fadbfc0b-e298-418a-b56c-8114699fff91"
}
我自己发现了问题所在。
Azure 门户中提供的令牌是 base64 编码的。所以必须解码它:
RCurl::base64Decode(key, mode="raw")
以便与 digest::hmac
函数一起使用。在此 hmac
函数中也必须指定 raw = TRUE
。