Skip to content

Instantly share code, notes, and snippets.

@mjhea0
Created May 1, 2013 18:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mjhea0/5497065 to your computer and use it in GitHub Desktop.
Save mjhea0/5497065 to your computer and use it in GitHub Desktop.
Sentiment Analysis via R
Create an app on https://dev.twitter.com/, make sure you leave Callback URL blank (so that it will return to a page where twitter shows you a PIN, useful during twitCred$handshake below. Don't worry yet, read on.)
Run these on R (on separate lines):
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
consumerKey <- "yourconsumerkey"
consumerSecret <- "yourconsumersecret"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
twitCred$handshake(cainfo="cacert.pem")
registerTwitterOAuth(twitCred)
And make sure you specify the PEM cert when calling the searchTwitter method:
tweets = searchTwitter("#abortion", n=200, cainfo="cacert.pem")
@loiyumba
Copy link

loiyumba commented May 8, 2014

twitCred$handshake(cainfo="cacert.pem")
To enable the connection, please direct your web browser to:
http://api.twitter.com/oauth/authorize?oauth_token=k4VYn1GRw33AVY1RPWfewokA4p2l2SrX41u591Au2s
When complete, record the PIN given to you and provide it here: 6597462
Error: Forbidden

I get the above error message :(

@loiyumba
Copy link

loiyumba commented May 8, 2014

I updated my rstudio version and it started working.
Thanks!

@StanSilas
Copy link

Now the twitter authentication has been changed. Currently "setup_twitter_oauth(key,secret,key2,secret2)" is being used.
What changes to the existing code are to be made?

@farahkc
Copy link

farahkc commented Mar 14, 2016

@StanSilas

consumerKey<-"ConsumerKey"
consumerSecret<-"ConsumerSecret"
access_token_secret<-"AccessTocketSecret"
access_token<-"AccessToken"
setup_twitter_oauth(consumerKey,consumerSecret,access_token,access_token_secret)

Once your authentication is setup, you can start scrapping tweeter:
tweets= searchTwitter("#trend", n=1500)

@sreelallalu
Copy link

can u give the java code

@dosa-chammandi
Copy link

setup_twitter_oauth(consumer_key=consumerkey, consumer_secret=consumerSecret, access_token=accessToken, access_secret=accessSecret)
[1] "Using direct authentication"
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment