Category Archives: Step 1 – R Authentication for Twitter

Step 1 – R Authentication for Twitter

Sentiment analysis – First Step

Getting a developer Twitter account

You can’t directly pull tweets from the twitter, to do so twitter comes up with OAuth handshake. It is necessary for every request to verify your app. Steps involved to pull tweets from twitter

Create an app in Twitter.

Go to the and log in with your twitter account. Click on Create New App.

You can name your Application whatever you want and also set Description on whatever you want. Twitter requires a valid URL for the website, you can just type in you won´t need to do anything more.

And just leave the Callback URL blank.

Twitter App

Download the curl Cert and save it at your default R folder – Communication with HTTPS, FTPS or other TLS servers using certificates that are signed by CAs present in the bundle, you can be sure that the remote server really is the one it claims to be.

> download.file(url=””, destfile=”cacert.pem”)

# Set constant request URL
> requestURL <- “;

# Set constant access URL
> accessURL = “;

# Set constant auth URL
> authURL = “;

Next step is to copy paste consumerKey and consumerSecret key from your twitter developer application. You can find the key in “Key and Access Token” tab.


> consumerKey = “xxxxxxxxxxxxxxxxxx”
> consumerSecret = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”

Create the authorization object by calling function OAuthFactory
> Cred <- OAuthFactory$new(consumerKey=consumerKey,

At this point we are ready to ask for our credentials to Twitter.

Twitter changed its API policy for data access. It has added a new step in the process which is handshake oauth
> Cred$handshake(cainfo=”cacert.pem”)
> Cred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”) )

In your R console you will see the following message instructing you to direct your web browser to the specified URL. There you will get a PIN code which you will have to type in your R console.

To enable the connection, please direct your web browser to:
When complete, record the PIN given to you and provide it here: xxxxxx

Copy and paste the URL provided above into your web browser, Twitter will generate a pin after valid authentication. Write it down in the R console to enable the connection

Verify that your new credential is working properly
> registerTwitterOAuth(Cred)
[1] TRUE

Save it for future use by downloading a Cred file at your default R folder
> save(Cred, file=”twitter authentication.Rdata”)

Start searching Twitter

Next time you open your R console, you don’t need to follow the same step, just set your directory where you save the Cred file. We assumed that, two file was in place “cacert.perm” and “twitteR_credentials”

Load Twitter Cred file and verify that the credential is working properly.
> load(“twitter authentication.Rdata”)
> registerTwitterOAuth(Cred)

Finally, pull some tweets of Swachh Bharat Abhiyan from the twiteer
> r_stats <- searchTwitter(“#SwachhBharat”, n=2000, cainfo=”cacert.pem”)


Tags: , , , , ,