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 @
  • Log On to with same credential
  • On top right corner in the drop down, Click on "My Applications" or goto 
  • 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:


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

Update Phone Number in Twitter Account:
  • Login to 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,

No comments:

Post a comment