Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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 twitter.com website. Next go to (https://dev.twitter.com) 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.

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

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.

credential<-

OAuthFactory$new(consumerKey="<Consumer_Key from twitter>" consumerSecret="Consumer_Secret from twitter",requestURL="https://api.twitter.com/oauth/request_token",accessURL="https://api.twitter.com/oauth/access_token",authURL="https://api.twitter.com/oauth/authorize")

Next we setup the handshake by entering the below command.

credential$handshake(cainfo="cacert.pem")

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

registerTwitterOAuth(logindetails)

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.

2 Comments
Labels in this area