Search code examples
rpermissionsgoogle-places-apihandshakewindows-firewall

open.connection failing in geocoding function


I'm currently running a geocoding function (using the google_places function in the googleway package). The function will run for a while (I have almost 3k locations), then throw the following error:

Error in open.connection(con, "rb") : 
  schannel: next InitializeSecurityContext failed: SEC_E_ILLEGAL_MESSAGE (0x80090326) - This error usually occurs when a fatal SSL/TLS alert is received (e.g. handshake failed). More detail may be available in the Windows System event log.

Having consulted the system event log, I found the following information:

The machine-default permission settings do not grant Local Activation permission for the COM Server application with CLSID 
{9BA05972-F6A8-11CF-A442-00A0C90A8F39}
 and APPID 
{9BA05972-F6A8-11CF-A442-00A0C90A8F39}

I'm not really sure what to do with this information. From my limited knowledge, it appears this is some sort of security/firewall issue. How should I go about giving R the permissions needed to run this function?

I am running Windows 10 with Windows Defender as antivirus/firewall. For reference, this is the function I am using for geocoding:

metro.locater <- function(lat, lon){

  library(googleway)

  #putting latitude and longitude into the same vector
  latlon <- c(lat, lon)

  #getting places result
  res <- google_places(location = latlon, 
                       place_type = "subway_station", radius = 50000,
                       rankby="distance",
                       key = "myKey")
  #condition handling
  if(res$status == 'OK'){
  closest <- res$results[1:3, ]

  return(closest)} else {
  try(return(res$status))
  }

}

Solution

  • I was able to fix the issue by using an adverb I'd used with another geocoding function that attempts to run the function 5 times when it fails to provide results. Given that this worked, it seems likely that this was just a transient error rather than a systemic issue.

    The adverb I used:

    safely <- function(fn, ..., max_attempts = 5) {
      function(...) {
        this_env <- environment()
        for(i in seq_len(max_attempts)) {
          ok <- tryCatch({
              assign("result", fn(...), envir = this_env)
              TRUE
            },
            error = function(e) {
              FALSE
            }
          )
          if(ok) {
            return(this_env$result)
          }
        }
        msg <- sprintf(
          "%s failed after %d tries; returning NULL.",
          deparse(match.call()),
          max_attempts
        )
        warning(msg)
        NULL
      }
    }
    

    Taken from Repeating values in loop until error disappears.