This article is just a small summary about the authentication process with Twitter. Its about creating the Twitter app and doing the handshake cause you have to do it every time you want to get data from Twitter with R.
Since Twitter released the Version 1.1 of their API a OAuth handshake is necessary for every request you do. So we have to verify our app.
First we need to create an app at Twitter.
Got to https://dev.twitter.com/ and log in with your Twitter Account.
Now you can see your Profile picture in the upper right corner and a drop-down menu. In this menu you can find “My Applications”.
Click on it and then on “Create new application”.
You can name your Application whatever you want and also set Description on whatever you want. Twitter requires a valid URL for the website, you can just type in http://test.de/ ; you won´t need it anymore.
And just leave the Callback URL blank.
Click on Create you´ll get redirected to a screen with all the OAuth setting of your new App. Just leave this window in the background; we´ll need it later
Before we go on, make sure you have installed the newest version of the twitteR package from github.
Therefore you can use the following code:
install.packages(c("devtools", "rjson", "bit64", "httr")) #RESTART R session! library(devtools) install_github("twitteR", username="geoffjentry") library(twitteR)
Now the twitteR package is up-to-date and we can use the new and very easy setup_twitter_oauth() function which uses the httr package.
The oAuth process:
First you have to get your api_key and your api_secret as well as your access_token and access_token_secret from your app settings on Twitter. Just click on the “API key” tab to see them.
api_key <- "YOUR API KEY" api_secret <- "YOUR API SECRET" access_token <- "YOUR ACCESS TOKEN" access_token_secret <- "YOUR ACCESS TOKEN SECRET" setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
And that´s it.
If you want to test your authentication just try to get some tweets with:
Now you can continue your Twitter research with one of my Twitter examples:
Or follow me on Twitter to stay up-to-date about everything connected to R and Data Science