0

I have written following script to extract tweets from twitter.

library("twitteR")
library("ROAuth")
library("RCurl")
library("httr")
download.file(url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")
ck <- '4QPOQJoiBgoGP7jU13Akxfp2E'
cs <- 'ued02VUpial3K8NW8foectZj1UFtsEfQetQJbYXD21uCj0ohJc'
ak <- '136316586-xWnCMrE5UYSzigTOwTVyR4NH4gfR01adpR5SF5IK'
as <- 'zsIj4lsDc5xKcDyofWkYijKnUY8Uj71sg1NfaIFKZbGc2'
reqURL<- 'https://api.twitter.com/oauth/request_token'
acURL <- 'https://api.twitter.com/oauth/access_token'
auURL <- 'https://api.twitter.com/oauth/authorize'
cred <- OAuthFactory$new(consumerKey=ck,consumerSecret=cs,requestURL=reqURL,accessURL=acURL,authURL=auURL)
cred$handshake(cainfo="cacert.pem")
save(cred, file="twitter_authentication.Rdata")
load("twitter_authentication.Rdata")
oauth_endpoints("twitter")
myapp <- oauth_app("twitter",key=ck,secret=cs)
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
setup_twitter_oauth(ck,cs)
search.string <- "#Bahubali"
no.of.tweets <- 10
tweets <- searchTwitter(search.string, n=no.of.tweets, lang="en")
tweets
write.csv(tweets, file='/home/horopter/tweet/CSVTweets.csv', row.names=F)

However I get this setup_twitter_oauth(ck,cs) [1] "Using browser based authentication" Waiting for authentication in browser... Press Esc/Ctrl + C to abort

and I get stuck there. website either says 'You do not have permissions.' or doesn't open at all. If I don't give a callback url, it doesn't authenticate, otherwise gets stuck again as aforementioned. Help me out here.

1 Answers1

0

I used tried using the same code and authentication token and here is the code that worked for me. The setup_twitter_oauth requires all ck, cs, ak, as arguments.

library("twitteR")
library("ROAuth")
library("RCurl") 
library("httr")
download.file(url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")
ck <- '4QPOQJoiBgoGP7jU13Akxfp2E'
cs <- 'ued02VUpial3K8NW8foectZj1UFtsEfQetQJbYXD21uCj0ohJc'
ak <- '136316586-xWnCMrE5UYSzigTOwTVyR4NH4gfR01adpR5SF5IK'
as <- 'zsIj4lsDc5xKcDyofWkYijKnUY8Uj71sg1NfaIFKZbGc2'
setup_twitter_oauth(ck,cs, ak, as)
search.string <- "#Bahubali"

This should setup your authentication once you have downloaded the .pem file. When it asks for authentication press 1.

KRC
  • 160
  • 7
  • `[1] "Using direct authentication" Error in check_twitter_oauth() : OAuth authentication error: This most likely means that you have incorrectly called setup_twitter_oauth()'` – GundamOfOasis Jul 18 '15 at 20:01
  • Guys, how do I convert data frame to status object? – GundamOfOasis Jul 21 '15 at 23:03
  • Sorry @SantoshKumarDesai didn't get your question? have you been able to setup the OAuth? – KRC Jul 22 '15 at 08:58
  • Yes I could setup Oauth, although logging in by setup_twitter_oauth() each time feels cumbersome. I stored retrieved tweets in a csv file by converting them from status object to data.frame. I'm unable to retrieve tweets from csv. I wanted to do `for( i in 1:3 ) { ch <- toString(i) ch <- paste(ch,".csv",sep="") if(i==1) B <- read.csv(ch,sep=',') else B <- rbind(B, read.csv(ch,sep=',')) } Bscore <- as.matrix(apply(B[, -1], 1, function(r) paste(names(B)[-1], r, sep=":", collapse=" "))) Bscore <- laply(B, function(t) t$getText())` – GundamOfOasis Jul 24 '15 at 10:26
  • It results in Error in t$getText : $ operator is invalid for atomic vectors. What can I do to retrieve my tweets from csv? I can provide data if you need more info. – GundamOfOasis Jul 24 '15 at 10:32