TwitteR,ROAuth和Windows:注册成功,但证书validation失败

我试图用twitteR来获得大量Twitter用户的关注者数量。 其他许多问题对我来说都是非常有用的,但就我所知,似乎没有一个与我的问题直接相关。

我可以注册我的OAuth证书到twitter R会话,但是我似乎无法做任何事情,我只是得到这个消息:

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

当我在不使用OAuth的情况下使用twitteR函数时,它们可以正常工作,不会出现任何错误或警告,但是我碰到了限制和受保护的帐户,我认为我可以通过OAuth避免这种情况。

以下是详细信息:

 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 

注册我的凭证后,如何才能使twitteRfunction正常工作?

更新 :尝试@ Btibert3的build议给出了同样的错误:

 > ## 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 

更新以下@ flz的build议添加cainfo="cacert.pem"到处都解决了我的问题:

 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) 

会议信息:

 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 

尝试:

 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 

每个获取/更新操作需要在后面追加一个cainfo="cacert.pem" 。 那个烦人的

我曾经收到过你上面描述的错误,但是这对我来说是有效的。

 #======================================================================================= ## 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 

更新这是一个临时解决scheme,请参阅@ flz的答案和我编辑的最终解决scheme的问题。

由于我发布的问题,我一直在使用这个简单的循环作为解决方法(适当的解决scheme仍然欢迎!)。 这将需要很长时间,但至less它会给我的数据。 也许别人会觉得它也有用。

 # 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 

ROauth 0.9.1似乎有一个问题,如果你使用这个版本,你应该看twitteR的开发者(它已经为我工作): http ://lists.hexdump.org/pipermail/ twitter-users-hexdump.org/2012-March/000075.html

其他答案集中在download.file()来更新cacert文件。 它不应该有所作为,但你可以尝试下载最新的Curl二进制文件,并使用它来更新cacert文件,像这样…

 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) 

我不知道我是否只是没有看到这个权利,但我认为这会导致一个问题,你的文件被保存到cacert.perm,但你告诉握手看看cacert.pem?

 > download.file(url =“http://curl.haxx.se/ca/cacert.pem”,
 > destfile =“cacert.perm”)
 > twitCred $ handshake(cainfo =“cacert.pem”)

你有没有尝试在你的握手中使用cacert.pem的完整path? 以下解决了我在Ubuntu中的问题:

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

即使在我的电脑上,这个错误是相当持久的,但是在所有函数中使用cainfo =“cacert.pm”可以消除这个问题,或者绕过它。 我不太确定

最后我得到了解决scheme,请尝试这种方法

 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) 

我想最快的方法是:

0)在Api.Twitter设置你的api键
1)使用hadley httr Twitter oauth
2)把你的twitter_token保存到RDS
3)上传到服务器或任何你想使用的地方
4)只需使用GET命令与此令牌
5)您可以从Twitter合法获得每小时30万个ID