Skip to content

Instantly share code, notes, and snippets.

@emhart
Created September 20, 2013 15:23
Show Gist options
  • Save emhart/6639202 to your computer and use it in GitHub Desktop.
Save emhart/6639202 to your computer and use it in GitHub Desktop.
How use use the twitter api with ROAuth and twitteR.
require(twitteR)
require(ggplot2)
library(ROAuth)
library(plyr)
### See this post on how to get your key and secret to put
### in below as strings: https://dev.twitter.com/discussions/631
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)
### Run this line and follow the instructions
twitCred$handshake()
### This will tell you if you're authorized, it should return true
registerTwitterOAuth(twitCred)
### Do your thing, in this case make a histogram of handle names
### and hashtag use.
rez <- searchTwitter("#ubcbdg",n=100)
users <- ldply(rez,function(x) return(x$screenName))
ggplot(users,aes(x=V1))+geom_histogram()+theme(axis.text.x = element_text(angle = 45, hjust = 1))+ylab("Count of tweets using #ubcbdg")+xlab("Twitter handle")
@ateucher
Copy link

It seems to be common on Windows to have to use the cainfo argument for any function that uses GET:

pem <- system.file("CurlSSL", "cacert.pem", package = "RCurl")
twitCred$handshake(cainfo=pem)

rez <- searchTwitter("#ubcbdg",n=100, cainfo=pem)

There's more info on the issue on this SO question: http://stackoverflow.com/questions/9916283/twitter-roauth-and-windows-register-ok-but-certificate-verify-failed

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