Skip to Tutorial Content

Twitter developer account


Check here to learn how to apply for a twitter developper account.

After that you can apply here. Make sure you complete the form with serious and valid information only, because your application needs to be approved by Twitter and it can take up to 48 hours before you are approved.

Twitter App

In this section you will learn how to create a twitter app.

Visit the Twitter Developers Site

The first thing you need to do is head on down to https://developer.twitter.com/. In order to create an account, all you need to do is click on the “Sign In” link at the top right.

Sign in with your Twitter Account

Next, sign in with the Twitter account you want to associate with your app.

Go to “Apps”

Once you’re logged in, click on the downwards arrow to the right of your Twitter image and select “Apps”. This is where all your registered Twitter apps will appear.

Create a New Application

If you are new to the Developers site you won’t see any applications registered. Either way, it’s time to create our first application. To do this, click on the big “Create an app” button.

Fill in your Application Details

Callback URL : http://127.0.0.1:1410/

Access Tokens

You can read the rtweet package vignette to learn how to obtain and use Twitter API access tokens for use in the rtweet package.

Create Your Access Token

Click the "Create my access token" button. This takes a few seconds, so if you don’t see the access tokens on the next screen, you may have to refresh the page a few times.

Choose What Access Type You Need

Change application type to ‘Read & Write’ to give the application permission to follow other accounts on your behalf. This will require that you verify your mobile phone within your twitter account.

Make a Note of Your OAuth Settings

Once you’ve done this, make a note of your OAuth settings.

  • Consumer Key
  • Consumer Secret
  • OAuth Access Token
  • OAuth Access Token Secret

It goes without saying that you should keep these secret. If anyone was to get these keys, they could effectively access your Twitter account.

Collecting Twitter Data

Authorizing Access

Authorization for your first time using rtweet

# load rtweet
library(rtweet)

# store api keys (these are fake example values; replace with your own keys)
api_key <- "afYS4vbIlPAj096E60c4W1fiK"
api_secret_key <- "bI91kqnqFoNCrZFbsjAWHD4gJ91LQAhdCJXCj3yscfuULtNkuu"
access_token <- "9551451262-wK2EmA942kxZYIwa5LMKZoQA4Xc2uyIiEwu2YXL"
access_token_secret <- "9vpiSGKg1fIPQtxc5d5ESiFlZQpfbknEN1f1m2xe5byw7"

# authenticate via web browser
token <- create_token(
  app = "NameOfYourApp",
  consumer_key = api_key,
  consumer_secret = api_secret_key,
  access_token = access_token,
  access_secret = access_token_secret)

Authorization in future R sessions

The create_token() function should automatically save your token as an environment variable for you. So next time you start an R session [on the same machine], rtweet should automatically find your token.

To make sure it works, restart your R session, run the following code, and again check to make sure the app name and api_key match.

## check to see if the token is loaded
get_token()

Searching Tweets

Search for 1000 (non-retweeted) tweets containing the covid19 hashtag. Note: Be aware that you can search for up to 15000 (non-retweeted) tweets per 15 minutes.

# search for 1000 tweets using the covid19 hashtag
covid19 <- search_tweets("#covid19", n=1000, include_rts = FALSE, retryonratelimit = TRUE)

# preview tweets data
covid19

# preview users data
users_data(covid19)

The analysis called "A global pandemic on twitter" by Jason Timm is using the rtweet package, check this here.

References

This course uses the rtweet documentation.

Acknowledgments

To cite this course:

Warin, Thierry. 2020. “Covid-19 Simulation: A Data Science Perspective.” doi:10.6084/m9.figshare.12020994.v1.

Collecting Twitter Data