Friday 18 July 2014

Connecting to Twitter's REST API v1.1 with R - Part2

This is in continuation to my last blog Connecting to Twitter's REST API v1.1 with R - Part1, where I have already explained setting up Twitter account to access Twitter APIs from R platform. Here, we will look into creating handshake between R Studio and Twitter APIs.

We require few other details from Twitter Application. Open your application and go to Details Tab:
We will use these tokens (Request URL,  Authorize URL, Access URL) while creating handshake with Twitter APIs from R.

The first and foremost important thing after this is to install R studio or simply R environment from here. I am not going deep into how to install R and other setups. 

Install Package in R console:
To access Twitter APIs from R, you must install few packages in R. Go to R console and write the following code:

install.Packages("twitteR"). #This will install other dependent packages like ROAuth, RCurl, and others.

Load Packages:
library(twitteR). #This will load the package required to access the Twitter APIs. It will load the dependent packages as well.

Create Authorization Object:


requestURL <-  "<your Request  URL as indicated in above figure>"
accessURL <-  "<your Access    URL as indicated in above figure>"
authURL      <-  "<your Authorize URL as indicated in above figure>"
consumerKey <-  "<your API key from API keys tab in Twitter App>" # See my blog Part1
consumerSecret <-  "<your API secret from API keys tab in Twitter App>" # See my blog Part1 

Create Handshake Object:

Cred <- OAuthFactory$new(consumerKey=consumerKey,
                         consumerSecret=consumerSecret,
                         requestURL=requestURL,
                         accessURL=accessURL,
                         authURL=authURL)

Download CA certificate as follows:
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")

Create Handshake using CA certificate:
Cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))

You will get a response as follows, if your handshake is successful:
To enable the connection, please direct your web browser to: 
https://api.twitter.com/oauth/authorize?..............................
When complete, record the PIN given to you and provide it here:

Copy the above URL and paste it in web browser. You will be asked to authorize the app:



Click on Authorize button. You will receive a key:
Copy this Key and paste it in the R console before hitting ENTER button.

Next, register this authorization:
registerTwitterOAuth(Cred)

This completes your authorization process. Now you are ready to go ahead in accessing the twitter APIs.

You can access any user's followers count as follows:
getUser("<user name>",cainfo="cacert.pem")$friendsCount

Now you can start your Data Mining and analysis using twitter data.

Thanks for reading my blog. Look forward for my next blog on R.

Regards
Rocky

1 comment:

  1. Hi Rocky, I am facing an issue in the final handshake step
    Cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
    I get the below error, could you pls help?
    Error in function (type, msg, asError = TRUE) : couldn't connect to host

    ReplyDelete