android 包 - R.attr 嵌套 class -- 使用 api 键
android package - R.attr nested class -- employing an api key
我尝试使用 R 中的 HTTR 包模拟 CURL 请求。这是为了 propbulica 的选举 API。
propublica.github.io/campaign-finance-api-docs/
命令行请求记录如下:
curl "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json" -H "X-API-Key: PROPUBLICA_API_KEY"
我在使用R中的模仿如下:
require(httr)
api_key <- "my key"
path <- "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json"
data <- GET(path, add_headers("X-API-Key", .headers = api_key))
content(data)
这个returns一个"forbidden"。
ProPublica 的 Derek Willis 告诉我我的密钥有效。
GET(path, config = list(token = api_key))
这是假设 "my key" 是一个 oauth 令牌环境。需要了解更多关于 API 的信息才能确定。有关密钥生成和 api 握手的信息,请参阅 httr 中的 github 示例。
我制作了 curlconverter
包来帮助解决这类问题:
library(curlconverter)
cmd <- 'curl "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json" -H "X-API-Key: PROPUBLICA_API_KEY"'
parsed_cmd <- straighten(cmd)
str(parsed_cmd)
## List of 1
## $ :List of 5
## ..$ url : chr "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json"
## ..$ method : chr "get"
## ..$ headers :List of 1
## .. ..$ X-API-Key: chr "PROPUBLICA_API_KEY"
## ..$ url_parts:List of 9
## .. ..$ scheme : chr "https"
## .. ..$ hostname: chr "api.propublica.org"
## .. ..$ port : NULL
## .. ..$ path : chr "campaign-finance/v1/2016/president/totals.json"
## .. ..$ query : NULL
## .. ..$ params : NULL
## .. ..$ fragment: NULL
## .. ..$ username: NULL
## .. ..$ password: NULL
## .. ..- attr(*, "class")= chr [1:2] "url" "list"
## ..$ orig_curl: chr "curl \"https://api.propublica.org/campaign-finance/v1/2016/president/totals.json\" -H \"X-API-Key: PROPUBLICA_API_KEY\""
## ..- attr(*, "class")= chr [1:2] "cc_obj" "list"
## - attr(*, "class")= chr [1:2] "cc_container" "list"
actual_function <- make_req(parsed_cmd)[[1]] # returns a list as it's vectorized
拨打电话 - 它应该“正常工作”
# actual_function() # not going to work here since it's not a real api key
看看里面有什么:
actual_function
## function ()
## httr::VERB(verb = "GET", url = "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json",
## httr::add_headers(`X-API-Key` = "PROPUBLICA_API_KEY"))
## <environment: 0x7f8d90aeee98>
它旨在与来自浏览器开发人员工具 windows 的 "Copy as cURL" 字符串一起使用 windows。
我尝试使用 R 中的 HTTR 包模拟 CURL 请求。这是为了 propbulica 的选举 API。
propublica.github.io/campaign-finance-api-docs/
命令行请求记录如下:
curl "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json" -H "X-API-Key: PROPUBLICA_API_KEY"
我在使用R中的模仿如下:
require(httr)
api_key <- "my key"
path <- "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json"
data <- GET(path, add_headers("X-API-Key", .headers = api_key))
content(data)
这个returns一个"forbidden"。
ProPublica 的 Derek Willis 告诉我我的密钥有效。
GET(path, config = list(token = api_key))
这是假设 "my key" 是一个 oauth 令牌环境。需要了解更多关于 API 的信息才能确定。有关密钥生成和 api 握手的信息,请参阅 httr 中的 github 示例。
我制作了 curlconverter
包来帮助解决这类问题:
library(curlconverter)
cmd <- 'curl "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json" -H "X-API-Key: PROPUBLICA_API_KEY"'
parsed_cmd <- straighten(cmd)
str(parsed_cmd)
## List of 1
## $ :List of 5
## ..$ url : chr "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json"
## ..$ method : chr "get"
## ..$ headers :List of 1
## .. ..$ X-API-Key: chr "PROPUBLICA_API_KEY"
## ..$ url_parts:List of 9
## .. ..$ scheme : chr "https"
## .. ..$ hostname: chr "api.propublica.org"
## .. ..$ port : NULL
## .. ..$ path : chr "campaign-finance/v1/2016/president/totals.json"
## .. ..$ query : NULL
## .. ..$ params : NULL
## .. ..$ fragment: NULL
## .. ..$ username: NULL
## .. ..$ password: NULL
## .. ..- attr(*, "class")= chr [1:2] "url" "list"
## ..$ orig_curl: chr "curl \"https://api.propublica.org/campaign-finance/v1/2016/president/totals.json\" -H \"X-API-Key: PROPUBLICA_API_KEY\""
## ..- attr(*, "class")= chr [1:2] "cc_obj" "list"
## - attr(*, "class")= chr [1:2] "cc_container" "list"
actual_function <- make_req(parsed_cmd)[[1]] # returns a list as it's vectorized
拨打电话 - 它应该“正常工作”
# actual_function() # not going to work here since it's not a real api key
看看里面有什么:
actual_function
## function ()
## httr::VERB(verb = "GET", url = "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json",
## httr::add_headers(`X-API-Key` = "PROPUBLICA_API_KEY"))
## <environment: 0x7f8d90aeee98>
它旨在与来自浏览器开发人员工具 windows 的 "Copy as cURL" 字符串一起使用 windows。