Search code examples
rtwitterstreamingtwitter-oauthdevtools

Errors when trying to stream from the Twitter API


I am trying to stream data from Twitter, by using StreamR and this guide https://github.com/pablobarbera/streamR

It worked recently, but now I get this error when I try to stream any data;

library(ROAuth)
library(streamR)

my_oauth <- list(consumer_key = "...",
   consumer_secret = "...",
   access_token="...",
   access_token_secret = "...")

getUsers(screen_names="LSEnews", oauth = my_oauth)

#Error in function (type, msg, asError = TRUE)  : 
  #Unknown SSL protocol error in connection to api.twitter.com:443

When I try to stream tweets the connection closes after 0 second

filterStream(file.name="trump-tweets.json", track="trump", timeout=120, oauth=my_oauth)
#Capturing tweets...
#Connection to Twitter stream was closed after 0 seconds with up to 0 tweets #downloaded.

I have re-generated my costumerkeys and access tokens on the Twitter developer site and also created a new app. I have also tried to update the ROAuth and streamR -package. However, when I try to update streamR from github, I get this error (I have installed the package before without any problems);

library(devtools)

#Loading required package: usethis
devtools::install_github("pablobarbera/streamR/streamR")

#Error in rethrow_call(c_processx_exec, command, c(command, args), stdin,  : 
#  Incorrect number of arguments (16), expecting 14 for 'processx_exec'

Which led me to this answer that I might need to update the processx package

https://github.com/r-lib/devtools/issues/2077

Which I have updated by

install.packages("processx")

But it did not solve the problem, I still get the same error when I try to update the StreamR package from Github. I have also tried to install the streamR-package through CRAN;

install.packages("streamR")
install.packages("ROAuth")

But it did not solve my primary problem about streaming tweets.

Any help is appreciated, thanks!


Solution

  • I was able to successfully run the code as per below (proving that it is indeed an issue with your setup and nothing has changed in the API or package itself).

    A couple notes:

    • I installed the github version of streamR
    • Notice that I couldn't run the function getUsers; I suspect that function is from the CRAN version
    • I've included the output of sessionInfo(); perhaps if you ensure you have the same package versions and R version as I do you will be able to run successfully or at least lead you in the direction
    library(streamR)
    #> Loading required package: RCurl
    #> Loading required package: bitops
    #> Loading required package: rjson
    #> Loading required package: ndjson
    
    my_oauth <- list(consumer_key = "MY_CONSUMER_KEY_HERE",
                     consumer_secret = "MY_CONSUMER_SECRET_HERE",
                     access_token="MY_ACCESS_TOKEN_HERE",
                     access_token_secret = "MY_ACCESS_SECRET_HERE")
    
    filterStream("tweets.json", track = c("Obama", "Biden"), timeout = 120, 
                 oauth = my_oauth)
    #> Capturing tweets...
    #> Connection to Twitter stream was closed after 120 seconds with up to 1146 tweets downloaded.
    
    getUsers(screen_names="LSEnews", oauth = my_oauth) #not a function I see
    #> Error in getUsers(screen_names = "LSEnews", oauth = my_oauth): could not find function "getUsers"
    
    # List all functions in this package
    ls("package:streamR")
    #> [1] "createOAuthToken" "filterStream"     "parseTweets"     
    #> [4] "readTweets"       "sampleStream"     "userStream"
    
    filterStream(file.name="trump-tweets.json", track="trump", timeout=120, oauth=my_oauth)
    #> Capturing tweets...
    #> Connection to Twitter stream was closed after 120 seconds with up to 5187 tweets downloaded.
    
    sessionInfo()
    #> R version 3.6.1 (2019-07-05)
    #> Platform: x86_64-apple-darwin15.6.0 (64-bit)
    #> Running under: macOS Catalina 10.15
    #> 
    #> Matrix products: default
    #> BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
    #> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
    #> 
    #> locale:
    #> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    #> 
    #> attached base packages:
    #> [1] stats     graphics  grDevices utils     datasets  methods   base     
    #> 
    #> other attached packages:
    #> [1] streamR_0.4.5   ndjson_0.7.0    rjson_0.2.20    RCurl_1.95-4.12
    #> [5] bitops_1.0-6   
    #> 
    #> loaded via a namespace (and not attached):
    #>  [1] Rcpp_1.0.2        knitr_1.25        magrittr_1.5     
    #>  [4] tidyselect_0.2.5  R6_2.4.0          rlang_0.4.0      
    #>  [7] ROAuth_0.9.6      stringr_1.4.0     highr_0.8        
    #> [10] dplyr_0.8.3       tools_3.6.1       data.table_1.12.2
    #> [13] xfun_0.9          htmltools_0.3.6   yaml_2.2.0       
    #> [16] digest_0.6.21     assertthat_0.2.1  tibble_2.1.3     
    #> [19] crayon_1.3.4      dtplyr_0.0.3      purrr_0.3.2      
    #> [22] glue_1.3.1        evaluate_0.14     rmarkdown_1.15   
    #> [25] stringi_1.4.3     compiler_3.6.1    pillar_1.4.2     
    #> [28] pkgconfig_2.0.3
    

    Created on 2019-10-01 by the reprex package (v0.3.0)