R OAuth for TwitteR

gCodeAdSense

The gadget spec URL could not be found
R language through package TwitteR is able to extract information from Twitter for Text Mining purposes. In order to get the connection between R console and Twitter work properly, you will need previously to establish a secure connection with Twitter. In this page you will find a full procedure to set this connection.

Getting a developer Twitter account

 First of all you must go to Twitter Developers page and sign up for a new account,
after that you should create a new application.

An application is a purpose for your connection, you will provide just a name, description and website address and you will get an access token and an access token secret. 

Keep these two codes for later use at R console, when asking four your R access credentials.

Getting a curl Certification

Open your R console and start by loading the following libraries

#
# Clear the previously used libraries
#
> rm(list=ls())
#
# Load the required R libraries
#
> library(twitteR)
> library(ROAuth)
> library(RCurl)

Download the curl Cert and save it at your default R folder

> download.file(url="http://curl.haxx.se/ca/cacert.pem",
destfile="cacert.pem")


Setting the Certification at Twitter

We will prepare the call to function OAuthFactory to get the auth object.

#
# Set constant requestURL
#
>  requestURL <- "https://api.twitter.com/oauth/request_token"
#
# Set constant accessURL
#
>  accessURL <- "https://api.twitter.com/oauth/access_token"
#
# Set constant authURL
#
> authURL <- "https://api.twitter.com/oauth/authorize"

In consumerKey field paste the access token you got from your twitter developer application.

> consumerKey <- "xxxxxxxxxxxxxxxxxx"

In consumerSecret field paste the access token you got from your twitter developer application.

> consumerSecret <- "xxxxxxxxxxxxxxxxxx"

Creating the authorization object by calling function OAuthFactory

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

At this point we are ready to ask for our credentials to Twitter. Let's go for it !  

Saving and using the Certification to connect to Twitter


# Asking for access
> twitCred$handshake(cainfo="cacert.pem")

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: 
https://api.twitter.com/oauth/authorize?oauth_token=xxxx
When complete, record the PIN given to you and provide it here: xxxxxx

Perfect ! Almost done.

Just verify that your new credential is working properly

> registerTwitterOAuth(twitCred)
[1] TRUE

And save it for future use by downloading a Cred file at your default R folder

> save(list="twitCred", file="twitteR_credentials")

Done !

Start searching Twitter

Now its time to use it. To do so, imagine you open your R console from scratch, nothing  loaded in memory.

We assume that you have placed at your R default console, files "cacert.perm" and "twitteR_credentials"
 
> library (twitteR)
> load("twitteR_credentials")
> registerTwitterOAuth(twitCred)

Get, for instance, tweets about United Airlines

s <- searchTwitter('#United', cainfo="cacert.pem")

Watch out !  When searching, do not forget using your CA Cert, otherwise you will get the following horrible error message. 

[1] "SSL certificate problem, verify that the CA cert is OK. Details:\nerror:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed"
Error en twInterfaceObj$doAPICall(cmd, params, "GET", ...) :
Error: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

For even further and up to date info, please refer to R package vignette for TwitteR.

   |\____/|      ___________
  / @  @ \    /
  ( >  º < )  <  use it gently !
  `>>x<<´     \___________
   /   O   \ 

You can post any comment about this article at cRomoData.com