Skip to Content
Author's profile photo Former Member

Setting up Twitter API to work with R

For getting twitter data in SAP PA we first need to configure the Twitter API to connect and work with R. The below document illustrates these steps in detail.

First get a twitter account if you don’t have one by registering on website. Next go to ( with the same credentials. Here we will be setting up the API. This API is required for authentication to search tweets from a third party application. It uses an industry standard process called OAuth. OAuth creates the handshake between twitter and R using something called as “Consumer Key” and  “Consumer Secret”.

First step is to create an Application. Go to ->My Applications and click “Create New Application”. Fill up your relevant details as per sample shown below. Click the check box that says “Allow this application to be used to Sign In with Twitter”.

PS: The following steps need to be done in the R environment using R-Studio or the R-GUI. No SAP PA access required for the below steps.


Once the application is saved you get the below screen with your Consumer Key and Consumer Secret. Finally go to the Settings tab and make sure that “Read, Write and Access direct messages” is set. Using “OAuth tool”  tab you can come back to get your Consumer key and Consumer secret information when required.


We will need the following packages for extracting the tweets. twitteR, RJSONIO, bitops and RCurl.

Install them and load them in the work space you are working on.

  • library(twitteR)
  • library(RJSONIO)
  • library(bitops)
  • library(RCurl)

For Windows user we need to provide new SSL certificates required for secure communication over the internet. Using below command downloads new certificates and saves them within the current working directory for R. The other statement will set the option globally and will be used in all RCurl calls.


options(RCurlOptions = list(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”)))

Next step is to set the credentials for login to twitter. For this we require the “Consumer Secret” and “Consumer Key” that we set up in step 1. Use the below URL and enter the Key and Secret where mentioned. This will store all the details in the credential object which will be saved in your local work-space.


OAuthFactory$new(consumerKey=”<Consumer_Key from twitter>” consumerSecret=”Consumer_Secret from twitter“,requestURL=”“,accessURL=”“,authURL=”“)

Next we setup the handshake by entering the below command.


On doing that we get the screen shown below:


It shows a URL with an authorization token. We need to copy that URL as it is and go to that URL from a web browser.

If done correctly it will take you to your twitter account (you may need to login)


Once logged in you will receive a new screen with a PIN on it (my PIN had seven digits). Take that PIN type them into the R-console window


That was the last step. The credentials are in place now. We can use “logindetails” in our SAP PA to do some text analysis.

For checking if the connection is working just put below command and see if you get true


options(RCurlOptions = list(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”)))

The return value of TRUE shows that the log in is working and ready to help you get data from Twitter.

Try and fetch some tweets.

tweetList <- searchTwitter(“#WT20”, n=100)

WT20.df = twListToDF(tweetList)

write.csv(climate.df, file=”WT20.csv”, row.names = F)

Examine the CSV file that got saved on your desktop. It will have details about the tweet, user and location.

Finally save the credential file so that we can load it for our sentiment analysis inside SAP PA as and when required.

save(credential, file=”twitter authentication.Rdata”)

We will need to load this file while creating the custom component in SAP PA.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      great contribution, thanks

      Author's profile photo M. van Foeken
      M. van Foeken

      Hi Bimal,

      Thanks for this great blog! I want to add something I bumped into: when I copy and pasted the string to store the credential variable I noticed a , is missing. Furthermore the installation of the twitteR package did not include the required package ROauth. Otherwise the 0AuthFactory function is not working.

      With kind regards,