首页 文章

使用R进行DocumentDB Web API访问

提问于
浏览
1

尝试使用R和PostMan连接到documentDB Web API时出现以下问题 .

在DocumentDB文档中,向Web API提问的方法是使用base64 hash编写Authorization标头 .

在R我试图计算签名并直接用邮递员测试 Headers . 但我每次都得到一个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)

“密钥”是从门户获得的主键 . 然后,按照Azure文档,我的 Headers 是:

type=master&ver=1.0&sig=< thebase64(hash) >

我将它粘贴到PostMan中, Headers 为x-ms-version,date和x-ms-date .

但它不起作用..

我现在被困住,有没有人有想法?我使用错误的R功能吗?错误的密钥,有没有办法获得有关不匹配的更多信息?

web 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"
}

1 回答

  • 1

    我发现自己错了 .

    Azure门户中提供的令牌是base64编码的 . 因此必须对其进行解码:

    RCurl::base64Decode(key, mode="raw")
    

    为了与 digest::hmac 函数一起使用它 . 在此 hmac 函数中指定 raw = TRUE 也是必须的 .

相关问题