-
-
Save jmcastagnetto/2253d846af1c60f5c0da to your computer and use it in GitHub Desktop.
require(RCurl) | |
require(XML) | |
# Let's make a special class | |
NISTBeaconResponse <- function (ts) { | |
if(!is.integer(ts) & | |
!inherits(ts, "POSIXct") & | |
!inherits(ts, "POSIXlt")) { | |
stop("We expected a unix timestamp as an integer or a POSIXct or POSIXlt value") | |
} | |
tsval <- ifelse(inherits(ts, "POSIXct") | inherits(ts, "POSIXlt"), | |
as.integer(ts), ts) | |
url <- paste0("https://beacon.nist.gov/rest/record/", tsval) | |
headers <- basicTextGatherer() # might be a good idea to use this to check the HTTP status code | |
response <- getURL(url, headerfunction=headers$update) | |
# use the following line instead, if you get cert validation errors | |
# response <- getURL(url, .opts = list(ssl.verifypeer = FALSE), headerfunction=headers$update) | |
beacon <- structure(as.data.frame(xmlToList(xmlParse(response, asText=TRUE))), | |
class="NISTBeaconResponse") | |
beacon$frequency <- as.integer(beacon$frequency) | |
beacon$timeStamp <- as.integer(beacon$timeStamp) | |
beacon$statusCode <- as.integer(beacon$statusCode) | |
beacon | |
} | |
# and a way to print it | |
print.NISTBeaconResponse <- function(response) { | |
if(!inherits(response, "NISTBeaconResponse")) { | |
stop("An object of type NISTBeaconResponse was expected") | |
} | |
breakline <- function(longline) { | |
lines <- gsub('(.{1,50})', '\\1\n', longline) | |
lines <- gsub('\n', '\n\t\t\t', lines, fixed=TRUE) | |
lines | |
} | |
output <- "\tNIST Randomness Beacon Response\n\t===============================\n\n" | |
output <- paste0(output, "\t* version:\t", response$version, "\n") | |
output <- paste0(output, "\t* frequency:\t", response$frequency, " seconds\n") | |
output <- paste0(output, "\t* seed:\t\t", breakline(response$seedValue), "\n") | |
output <- paste0(output, "\t* status:\t", response$statusCode, "\n") | |
output <- paste0(output, "\t* timestamp:\t", as.POSIXct(response$timeStamp, origin="1970-01-01"), | |
" (", response$timeStamp,")\n") | |
output <- paste0(output, "\t* output:\t", breakline(response$outputValue), "\n") | |
output <- paste0(output, "\t* prev value:\t", breakline(response$previousOutputValue), "\n") | |
output <- paste0(output, "\t* signature:\t", breakline(response$signatureValue), "\n") | |
cat(output) | |
} | |
# test the code | |
ts <- Sys.time() | |
beacon <- NISTBeaconResponse(ts) | |
print(beacon) |
Check your RCurl or curl configuration. The error message seems to originate with a problem related to the trusted certs. The solution described in http://stackoverflow.com/questions/17411313/ssl-verification-causes-rcurl-and-httr-to-break-on-a-website-that-should-be-le might help you.
On Ubuntu 12.04 LTS and 14.04 LTS (64-bit, both) which is what I use, could not reproduce your error.
For those that for unknown reasons do not have the local certs needed to validate the HTTPS connection to NIST, try replacing line #15 above with:
response <- getURL(url, .opts = list(ssl.verifypeer = FALSE), headerfunction=headers$update)
Note: Updated the gist with a comment to include this modification
Here's the link to that site's certificate:
https://beacon.nist.gov/certificate/beacon.cer
It could probably be explicitly added to the function somehow.
When I run this I get the error:
Error in function (type, msg, asError = TRUE) : SSL certificate problem: unable to get local issuer certificate Called from: function (type, msg, asError = TRUE) { if (!is.character(type)) { i = match(type, CURLcodeValues) typeName = if (is.na(i)) character() else names(CURLcodeValues)[i] } typeName = gsub("^CURLE_", "", typeName) fun = (if (asError) stop else warning) fun(structure(list(message = msg, call = sys.call()), class = c(typeName, "GenericCurlError", "error", "condition"))) }(60L, "SSL certificate problem: unable to get local issuer certificate", TRUE) Browse[1]> print(beacon) Error during wrapup: object 'beacon' not found Browse[1]>