DocumentDB web API access with R

馋奶兔 提交于 2019-12-11 13:39:29

问题


I have the following issue when trying to connect to the documentDB web API with R and PostMan.

In the DocumentDB documentation the way to ask something to the web API is to compose an Authorization header with base64 hash.

In R I'm trying to compute the signature and test the header directly with postman. But I get every time a http 401. Here is my R code:

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)

the "key" is the primary key got from the portal. And then, following the Azure documentation, my header is:

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

I'm pasting that into PostMan with the headers x-ms-version, date and x-ms-date.

But it'is not working..

I'm stuck now, does anyone have an idea? Am I using a wrong R function? A wrong key, is there a way to get more information about the mismatch?

The web api response is :

{
  "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:


I found what was wrong by myself.

The token given in the Azure portal is base64 encoded. So It is mandatory to decode it:

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

in order to use it with the digest::hmac function. It is also mandatory to specify raw = TRUE within this hmac function.



来源:https://stackoverflow.com/questions/37097140/documentdb-web-api-access-with-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!