username <- "username@gmail.com" password <- "password_here" loginURL <- "https://accounts.google.com/ServiceLogin" authenticateURL <- "https://accounts.google.com/accounts/ServiceLoginAuth" require(RCurl) ch <- getCurlHandle() curlSetOpt(curl = ch, ssl.verifypeer = FALSE, useragent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13", timeout = 60, followlocation = TRUE, cookiejar = "./cookies", cookiefile = "./cookies") ## do Google Account login loginPage <- getURL(loginURL, curl = ch) ### NEW WAY: grab all the form field values using XPath require(XML) loginPageTree <- htmlParse(loginPage, asText = TRUE) inputNodes <- getNodeSet(doc = loginPageTree, path = "//form[@id = 'gaia_loginform']//input") inputNames <- lapply(inputNodes, function(x) { xmlGetAttr(x, "name") }) inputValues <- lapply(inputNodes, function(x) { xmlGetAttr(x, "value") }) names(inputValues) <- inputNames inputValues[["Email"]] <- username inputValues[["Passwd"]] <- password authenticatePage <- postForm(authenticateURL, .params = inputValues, curl = ch) ### OLD WAY: use regular expressions to get the needed "GALX" value ## require(stringr) ## galx.match <- str_extract(string = loginPage, ## pattern = ignore.case('name="GALX"\\s*value="([^"]+)"')) ## galx <- str_replace(string = galx.match, ## pattern = ignore.case('name="GALX"\\s*value="([^"]+)"'), ## replacement = "\\1") ## authenticatePage <- postForm(authenticateURL, .params = list(Email = username, Passwd = password, GALX = galx), curl = ch) ## get Google Insights results CSV insightsURL <- "http://www.google.com/insights/search/overviewReport" resultsText <- getForm(insightsURL, .params = list(q = "Sarah Palin", cmpt = "q", content = 1, export = 1), curl = ch) if(isTRUE(unname(attr(resultsText, "Content-Type"))[1] == "text/csv")) { ## got CSV file ## create temporary connection from results tt <- textConnection(resultsText) resultsCSV <- read.csv(tt, header = FALSE) ## close connection close(tt) } else { ## something went wrong ## probably need to log in again? }