我正在尝试使用R从Localytics中提取数据。以下是我正在使用的代码片段:
library(httr)
localytics_url = 'https://api.localytics.com/v1/query'
r <- POST(url = localytics_url,
body=list(
app_id=app_id,
metrics=c("users","revenue"),
dimensions=c("day","birth_day"),
conditions=list(
day=c("between", "2015-02-01", "2015-04-01")
)
),
encode="json",
authenticate(key,secret),
accept("application/json"),
content_type("application/json")
)
stop_for_status(r)
content(r)
但是我从内容中获得的输出是二进制的,而不是json。我糊涂了。此外,如果我尝试查看对象'r',我会看到
Response [https://api.localytics.com/v1/query]
Date: 2015-04-14 15:18
Status: 200
Content-Type: application/vnd.localytics.v1+hal+json;type=ResultSet; charset=utf-8
Size: 1.02 MB
<BINARY BODY>
我不明白为什么它是二元体或如何将其转换回来。任何人都可以给我任何帮助/线索吗?
我还使用以下代码尝试使用Rcurl:
cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")
object <- getForm(uri=localytics_url, app_id=app_id, metrics="customers", dimensions="day", conditions = toJSON(list(day=c("between", "2015-01-01", "2015-04-09"))), .opts=curlOptions(userpwd=sprintf("%s:%s", key, password))
但是会产生错误
Error in function (type, msg, asError = TRUE) :
SSL certificate problem: unable to get local issuer certificate
所以我有点难过。
######## 2015年4月15日新增首先感谢MrFlick迄今为止的帮助。我得到它与
一起工作contents=content(r, as="text")
非常感谢你的帮助。我(我想)以前尝试过,然后继续尝试使用fromJSON将其提取为R数据格式,但我使用的是rjson库,而jsonlite包对我有效。
感谢您的耐心等待。
答案 0 :(得分:1)
以下是有关如何获取数据的完整代码示例,然后提取结果并将其作为表格查看。
library(httr)
library(jsonlite)
response <- POST(url = 'https://api.localytics.com/v1/query',
body=list(
app_id='APP_ID',
metrics='sessions',
conditions=list(
day=c("between", format(Sys.Date() - 31, "%Y-%m-%d"), format(Sys.Date() - 1, "%Y-%m-%d"))
),
dimensions=c('new_device','day')
),
encode="json",
authenticate('KEY','SECRET'),
accept("application/json"),
content_type("application/json"))
stop_for_status(response)
// Convert the content of the result to a string, you can load with jsonlite
result <- paste(rawToChar(response$content), collapse = "")
// Useful to print your result incase you are getting any errors
print(result)
// Load your data with jsonlite
document <- fromJSON(result)
// The results tag contains the table of data you need
View(document$results)