代码之家  ›  专栏  ›  技术社区  ›  playforest

使用rcurl连接到BI工具的REST API

  •  0
  • playforest  · 技术社区  · 9 年前

    我试图从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文档位于受密码保护的页面后面,因此我无法链接它。如果需要其他信息,请告诉我。

    2 回复  |  直到 9 年前
        1
  •  2
  •   efic1    9 年前

    hrbrmstr完全正确!您应该生成两个api调用,第一个是对用户进行身份验证,第二个是查询数据。

    下面是使用来自R的Datorama查询API的一个完整的工作示例。如有任何其他问题,请随时联系Datroama支持部门。

    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")
    
        2
  •  1
  •   hrbrmstr    9 年前

    我没有访问他们的API的权限,如果他们有一个免费层,我可能会为这个服务编写一个小包装包。话虽如此,

    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")
    

    authorization_token 和编码字符串。

    然后你很可能需要用 每一个 随后的API调用(可能有一个超时,需要重新支持初始身份验证)。

    authenticate() 用于HTTP基本身份验证,而不是API JSON/REST身份验证中的此类身份验证。

    除了使用令牌身份验证之外,实际的API调用看起来很好。