15

I'm trying to get the number of followers of a large number of Twitter users with twitteR. Many of the other questions posted have been very useful in getting me this far, but none seem to be directly relevant to my problem, so far as I can see.

I can register my OAuth credentials to a twitter R session, but then I can't seem to do anything at all, I just get is this message:

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 fail

When I use the twitteR functions without using OAuth they work fine with no errors or warnings, but I bump into limits and protected accounts, which I thought I could avoid with OAuth.

Here are the details:

library(twitteR)
library(ROAuth)
library(RCurl)
#
# Here's how I register my credentials
#
requestURL <-  "https://api.twitter.com/oauth/request_token"
accessURL =    "https://api.twitter.com/oauth/access_token"
authURL =      "https://api.twitter.com/oauth/authorize"
consumerKey =   "------------"
consumerSecret = "-----------"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                             consumerSecret=consumerSecret,
                             requestURL=requestURL,
                             accessURL=accessURL,
                             authURL=authURL)
download.file(url="http://curl.haxx.se/ca/cacert.pem",
              destfile="cacert.pem")
twitCred$handshake(cainfo="cacert.pem")
To enable the connection, please direct your web browser to: 
https://api.twitter.com/oauth/authorize?oauth_token=xxxx
When complete, record the PIN given to you and provide it here: xxxxxx
registerTwitterOAuth(twitCred)
[1] TRUE
# so the OAuth bit appears to be ok...
#
# save it for a future sessions...
save(list="twitCred", file="twitteR_credentials")
# works, in future I can just
load("twitteR_credentials")
registerTwitterOAuth(twitCred)
#
# try to get follower numbers, here's where it goes south
me <- getUser("Rbloggers")
me$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
#
# another method, same problem
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
# 
# Here are the packages and versions I'm using
sessionInfo() 
R version 2.14.1 (2011-12-22)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ROAuth_0.9.2      digest_0.5.1      twitteR_0.99.19   rjson_0.2.6       RCurl_1.91-1.1   
 [6] bitops_1.0-4.1    igraph_0.5.5-4    topicmodels_0.1-4 tm_0.5-7          slam_0.1-23      
[11] modeltools_0.2-18 lasso2_1.2-12    

loaded via a namespace (and not attached):
[1] tools_2.14.1

How can I get the twitteR functions working after I register my credentials?

UPDATE: Trying @Btibert3's suggestion gives the same error:

> ## Authenticate with Twitter = this is an important peice of code
> registerTwitterOAuth(cred)
[1] TRUE
> ##########################################################################
> ## lets test out what our session limits look like
> ##########################################################################
> rate.limit <- getCurRateLimitInfo()
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

UPDATE following @flz's suggestion to add cainfo="cacert.pem" everywhere has fixed my problem:

rate.limit <- getCurRateLimitInfo( cainfo="cacert.pem")
rate.limit
                             resource limit remaining               reset
1                  /lists/subscribers   180       180 2013-03-27 09:35:37
2                         /lists/list    15        15 2013-03-27 09:35:37
3                  /lists/memberships    15        15 2013-03-27 09:35:37
4                   /lists/ownerships    15        15 2013-03-27 09:35:37
5                /lists/subscriptions    15        15 2013-03-27 09:35:37
6                      /lists/members   180       180 2013-03-27 09:35:37
7             /lists/subscribers/show    15        15 2013-03-27 09:35:37
8                     /lists/statuses   180       180 2013-03-27 09:35:37
9                         /lists/show    15        15 2013-03-27 09:35:37
10                /lists/members/show    15        15 2013-03-27 09:35:37
11     /application/rate_limit_status   180       179 2013-03-27 09:35:37 (etc)

Session Info:

sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ROAuth_0.9.2   digest_0.6.3   twitteR_1.1.0  rjson_0.2.12   RCurl_1.95-4.1 bitops_1.0-5  

loaded via a namespace (and not attached):
[1] tools_2.15.3
Community
  • 1
  • 1
Ben
  • 41,615
  • 18
  • 132
  • 227
  • did you ever get round this on your system? I would be keen to know if the solution I posted works on your system (as I was contributing [to a function](http://stackoverflow.com/a/15047013/1478381) which needs this to work and be robust). – Simon O'Hanlon Mar 12 '13 at 09:43
  • Thanks, yes, got it going by sprinkling `cainfo="cacert.pem"` through all the functions. – Ben Mar 27 '13 at 09:23

10 Answers10

9

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
Btibert3
  • 38,798
  • 44
  • 129
  • 168
  • 3
    thanks for sharing your script, it doesn't seem to make any difference for me... perhaps it's a problem beyond `R`? – Ben Mar 28 '12 at 23:54
  • when you run the line that is registerTwitterOAuth(cred), do you return a False? – Btibert3 Mar 29 '12 at 01:25
  • regardless, even if you do not authenticate, you can still query a good number of data elements from the Twitter API. – Btibert3 Mar 29 '12 at 01:26
  • Yes, `registerTwitterOAuth()` returns `TRUE` using your method and my method. As an alternative, I have a loop with a 60 s sleep interval running at the moment. Probably take a couple of days to get what I want though! – Ben Mar 29 '12 at 04:57
6

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.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
flz
  • 122
  • 2
  • 6
  • This seems to have done it, thanks. When I do `s <- searchTwitter('#caa2013')` I get the error, but when I do `s <- searchTwitter('#caa2013', cainfo="cacert.pem")` it works fine. – Ben Mar 27 '13 at 09:17
  • 1
    just to add to what @Ben said, this solution didn't work for me until I updated [`cacert.pem`](http://curl.haxx.se/ca/cacert.pem) by following that link and saved it in my working directory. – zap2008 Jun 06 '13 at 21:37
  • @zap2008 Where should I place cacert.pem file? – Ricardo Nov 17 '13 at 00:11
  • @zap2008 does not work either, I finally disable security with curl_setopt($feed, CURLOPT_SSL_VERIFYHOST, 0) and curl_setopt($feed, CURLOPT_SSL_VERIFYPEER, 0); – Ricardo Nov 18 '13 at 15:37
2

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
Ben
  • 41,615
  • 18
  • 132
  • 227
1

There seems to be a problem with ROauth 0.9.1, if you use that version you should take a look at this post by the developer of twitteR (it has worked for me): http://lists.hexdump.org/pipermail/twitter-users-hexdump.org/2012-March/000075.html

1

The other answers focus on download.file() to update a cacert file. It shouldn't make a difference, but you could try downloading the latest Curl binary and using it to update the cacert file like so...

url <- "http://curl.askapache.com/download/curl-7.23.1-win64-ssl-sspi.zip"
tmp <- tempfile( fileext = ".zip" )
download.file(url,tmp)
unzip(tmp, exdir = tempdir())

system( paste0( tempdir() , "/curl http://curl.haxx.se/ca/cacert.pem -o " , tempdir() , "/cacert.pem" ) )

# You can use this freshly downloaded cacert file and you can also set ssl.verifypeer = FALSE
twitCred$handshake( cainfo = paste0( tempdir() , "/cacert.pem" ) , ssl.verifypeer = FALSE )
registerTwitterOAuth(Cred)
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
1

I'm not sure if I'm just not seeing this right, but I'm thinking it would cause an issue that your file is saved to cacert.perm but you're tell the handshake to look in cacert.pem?

> download.file(url="http://curl.haxx.se/ca/cacert.pem",
>              destfile="cacert.perm")
> twitCred$handshake(cainfo="cacert.pem")
  • Good catch, definitely a typo there! But I don't think that was the main issue. – Ben Mar 27 '13 at 09:20
0

Have you tried using the the full path to the cacert.pem in your handshake? The following solved the issue for me in Ubuntu:

twitCred$handshake(cainfo="/etc/ssl/certs/cacert.pem")

ajsmith007
  • 106
  • 5
0

This error is quite persistent even on my pc, but using the cainfo="cacert.pm" in all functions eliminates this issue, or goes around it. Am not too sure

0

finally i got the solution, please try this method

library(devtools)
install_github("twitteR", username="geoffjentry")
library(twitteR)

api_key = "aaa"
api_secret = "bbb"
access_token = "ccc"
access_token_secret = "ddd"
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
Selcuk Akbas
  • 711
  • 1
  • 8
  • 20
0

I think the fastest way:

0) Setup your api keys at Api.Twitter
1) Use hadley httr Twitter oauth
2) Save you twitter_token to RDS
3) Upload to server or anywhere where you would like to use
4) Simply use GET command with this token
5) You can get 300 000 ids per hour legally from Twitter

RRR
  • 103
  • 1
  • 7
  • if the limit is not enough.. create 10 users and RDS token file. (Switch between tokens). With that you can reach 3M ids per hour. Or contact with the Twitter DEV team. But I think, 3M/ hour far enough. ;) – RRR Oct 06 '16 at 13:19