I would like to connect to Twitter's Streaming API using RCurl in R, and also be able to filter keywords. However, new restrictions on authorization in Twitter API v1.1 is making using RCurl difficult.
Before, code could go something like this taken from this page:
getURL("https://stream.twitter.com/1/statuses/filter.json",
userpwd="Username:Password",
cainfo = "cacert.pem",
write=my.function,
postfields="track=bruins")
But now, Twitter's new API is making users authorize with OAuth. I have a token and secret, I just need to place it in this code for authorization.
Thanks!
You can do it with pacakge ROAuth
. I assume you have registered your app with Twitter and have an API key. I pieced this together from other questions on Stack Overflow (that question and associated answers also contains some links to other contributing questions) and the documentation for package ROAuth
and twitteR
.
library(RCurl)
library(twitteR)
library(ROAuth)
requestURL <- "https://api.twitter.com/oauth/request_token"
accessURL = "http://api.twitter.com/oauth/access_token"
authURL = "http://api.twitter.com/oauth/authorize"
consumerKey = "myconsumerkeystring"
consumerSecret = "myconsumersecretstring"
Cred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=requestURL,
accessURL=accessURL,
authURL=authURL)
#The next command provides a URL which you will need to copy and paste into your favourite browser
#Assuming you are logged into Twitter you will then be provided a PIN number to type into the R command line
Cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") )
# Checks that you are authorised
registerTwitterOAuth(Cred)
I believe that use of the streaming API is handled by the package streamR
HTH