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

Sunday 13 July 2014

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

Welcome to my first ever blog. Since past few days, while doing social media analysis on Twitter, I ran into some problem accessing the Twitter APIs. There is nothing great in it. But if you want to access Twitter APIs for free from India or few other countries where Twitter doesn't have telecom carrier partner, using Twitter APIs is complex. I could find lot of documents in WEB giving step-by-step process to access Twitter APIs, but nowhere I could find the solution of the errors that I faced while accessing the APIs. After searching a lot and trying out, I finally succeeded  in accessing Twitter APIs from R Studio. This encouraged me to write a blog for you.

Minimum Requirement to understand Twitter APIs access from R is:

  • Basic Understanding of R 
  • Install R. I am using R Studio here
New Twitter API has strict authorization:
Newest Twitter API requires a strict authorization procedure to utilize any of its features. In Version 1.1 of their API a OAuth handshake from R is necessary for every request you do. 

Create a Twitter App:
Follow the steps in the given order to create and authorize your app:
  • You mus have a Twitter Account. If not create one @ twitter.com.
  • Log On to https://dev.twitter.com with same credential
  • On top right corner in the drop down, Click on "My Applications" or goto https://apps.twitter.com/ 
  • Click on "Create New App" 

  • Give a Name to your App
  • Give a Description
  • You must create your own website to create this App. There are lot of service provider who allows you to create your own free website. Search in Google, create your own website and publish it. Mention the same website in the above field
  • No need to mention callback URL
  • Read the agreement and click on Agree button. 
  • Click on "Create your Twitter Application"
  • Click on Permissions Tab:
Change the permission to "Read Write and Access direct messages".  Without changing the permission to Read Write and direct messages, you cant access the APIs. Changing access to this requires your phone number to be updated in twitter account. You will get an error as follows:

Error

You must add your mobile phone to your Twitter profile before granting your application write capabilities

Update Phone Number in Twitter Account:
  • Login to twitter.com and goto settings.
  • Click on Mobile and input your country, mobile number and service provider(if seen on screen)
  • Click on Update. If you are in a country where Twitter doesn't have a connection to any telecom carrier, you will receive a message as follows:

Sorry, we don't have a connection to your carrier yet!

  • This is where you fall in trouble. I tried to read several blogs to find the answer and finally found a workaround for it:
In order to use this workaround, you need to have a smart phone and install the Twitter mobile application. This workaround works for me in iOS - I could not do on Android phone but other people reported in the Twitter forum that it was possible for them.
After installing the Twitter mobile application, go to the tab on the right, it's the screen showing your user details. (Following snapshots are copied from web)
  • Click on Settings:
  • Click on Your Account:

  • Click on Security:

  • Click on Add Phone:

  • Enter your phone number and save

Now you will be able to switch to Twitter application write access, although the Twitter websites doesn't accept your phone number:


Select Read Write and Access direct messages and click on Update settings. You can now generate your access token as follows:

Click on Create my access token to generate keys. The keys are then generated as follows:

These generated keys will be used in R to create handshake between R Studio and Twitter APIs.

To Be Continued... in my next Blog(Part2) : Connecting to Twitter's REST API v1.1 with R - Part2


Thanks for reading this Blog,
Rocky