我正在尝试从R中直接连接到BI工具的API.API文档列出了下面的curl命令以获取身份验证令牌:
curl -X POST -H "Content-Type: application/json" -d
'{
"email": "your@email.com",
"password": "your_password"
}'
https://app.datorama.com/services/auth/authenticate
此外,下面是可用于查询数据的JSON查询示例:
{
"brandId": "9999",
"dateRange": "CUSTOM",
"startDate": "2016-01-01",
"endDate": "2016-12-31",
"measurements": [
{
"name": "Impressions"
}
],
"dimensions": [
"Month"
],
"groupDimensionFilters": [],
"stringDimensionFilters": [],
"stringDimensionFiltersOperator": "AND",
"numberDimensionFiltersOperator": "AND",
"numberMeasurementFilter": [],
"sortBy": "Month",
"sortOrder": "DESC",
"topResults": "50",
"groupOthers": true,
"topPerDimension": true,
"totalDimensions": []
}
我正在尝试1)将上面的curl命令转换为R以获取所需的身份验证令牌,以及2)通过上面的JSON脚本查询数据。
到目前为止,我已尝试使用httr
库,如下所示:
library(httr)
r <- POST('https://app.datorama.com/services/auth/authenticate',
body = list(
brandId = "9999",
dateRange = "CUSTOM",
measurements = list(name="Impressions"),
dimensions = list(name="Month"),
startDate = "2016-01-01",
endDate = "2016-12-31"
),
encode = "json",
authenticate("username", "password"))
无济于事。
API文档位于受密码保护的页面后面,因此我无法链接它。如果需要其他信息,请与我们联系。
答案 0 :(得分:2)
hrbrmstr是完全正确的!你应该生成两个api调用,第一个是验证用户,第二个是查询数据。
以下是使用R的Datorama查询API的完整工作示例。如有任何其他问题,请随时联系Datorama支持。
library(httr)
res <- POST("https://app.datorama.com/services/auth/authenticate",
body=list(email="your@email.com",
password="your_password"),
encode="json")
token <- content(res)$token
res_query <- POST(paste("https://app.datorama.com/services/query/execQuery?token=",token, sep=""),
body = list(
brandId = "9999",
dateRange = "CUSTOM",
measurements = list(list(name = "Impressions")),
dimensions = list("Month"),
startDate = "2016-01-01",
endDate = "2016-12-31"
),
encode = "json")
cat(content(res_query, "text"), "\n")
答案 1 :(得分:1)
我无法访问他们的API,如果他们有免费套餐,他们会为这项服务编写一个小包装pkg。话虽如此,
curl -X POST \
-H "Content-Type: application/json" \
-d '{ "email": "your@email.com",
"password": "your_password" }'
转换为:
library(httr)
res <- POST("https://app.datorama.com/services/auth/authenticate",
body=list(email="your@email.com",
password="your_password"),
encode="json")
他们也没有在线免费获取他们的应用API文档,但我会假设它发回了一些带有authorization_token
和编码字符串的JSON响应。
然后,您很可能需要使用每个后续API调用传递该结果(并且可能需要重新启动初始身份验证的超时)。
authenticate()
用于HTTP基本身份验证,不适用于此类API in-JSON / REST身份验证。
除了使用令牌身份验证之外,您的实际API调用看起来很好。