TwitteR, ROAuth and Windows: register OK, but certificate verify failed

Solution 1:

I had received the error you described above in the past, but this has worked for me.

#=======================================================================================
## ON windows, we need to dowload the certificate for OAUTH
## NOTE:  you will need to setup an app on Twitter
## dev.twitter.com <- get your KEY/SECRET
#=======================================================================================


##########################################################################
## Load packages
##########################################################################

library(twitteR)
library(ROAuth)

## set the directory
setwd("~/your/directory/here")


## Windows users need to get this file
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")



##########################################################################
## Authenticate with Twitter
##########################################################################

## authenticate with the API
## requires that you have registered an app
KEY <- "KEY"
SECRET <-"SECRET"


## create an object that will save the authenticated onbject -- we can for later sessions
## will need to navigate to website and type in data to generate the file
## NOTE:  Only need to do this part once!!!
cred <- OAuthFactory$new(consumerKey = KEY, 
    consumerSecret = SECRET,
    requestURL = "https://api.twitter.com/oauth/request_token", 
    accessURL = "https://api.twitter.com/oauth/access_token", 
    authURL = "https://api.twitter.com/oauth/authorize")
cred$handshake(cainfo="cacert.pem")


## load the cred object in later sessions and simply pass to the registerTwitterOAuth
## After this file is saved, you only need to load the cred object back into memory
save(cred, file="twitter authentication.Rdata")


## Authenticate with Twitter = this is an important peice of code
registerTwitterOAuth(cred)


##########################################################################
## lets test out what our session limits look like
##########################################################################
rate.limit <- getCurRateLimitInfo()

## If return 350, Authenticated session = more API calls allowed / hour
rate.limit$hourlyLimit
rate.limit$remainingHits
rate.limit$resetTime

Solution 2:

Try:

getUser("Rbloggers")$followersCount
Error in function (type, msg, asError = TRUE)  : 
  SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
getUser("Rbloggers",cainfo="cacert.pem")$followersCount
[1] 2752

Every get/update action needs a cainfo="cacert.pem" append behind. That annoy.

Solution 3:

UPDATE This was an interim solution, see @flz's answer and my edited question for the final solution.

Since I posted the question I've been using this simple loop as a workaround (proper solutions are still welcome!). It's going to take a long time, but at least it'll give me the data. Perhaps someone else will find it useful also.

# load library
library(twitteR)
#
# Search Twitter for your term
s <- searchTwitter('#rstats', n=1500) 
# convert search results to a data frame
df <- do.call("rbind", lapply(s, as.data.frame)) 
# extract the usernames
users <- unique(df$screenName)
users <- sapply(users, as.character)
# make a data frame for the loop to work with 
users.df <- data.frame(users = users, 
                       followers = "", stringsAsFactors = FALSE)
#
# loop to populate users$followers with follower 
# count obtained from Twitter API
for (i in 1:nrow(users.df)) 
    {
    # tell the loop to skip a user if their account is protected 
    # or some other error occurs  
    result <- try(getUser(users.df$users[i])$followersCount, silent = TRUE);
    if(class(result) == "try-error") next;
    # get the number of followers for each user
    users.df$followers[i] <- getUser(users.df$users[i])$followersCount
    # tell the loop to pause for 60 s between iterations to 
    # avoid exceeding the Twitter API request limit
    print('Sleeping for 60 seconds...')
    Sys.sleep(60); 
    }
#
# Now inspect users.df to see the follower data