Obtaining and using access tokens

rtweet: Collecting Twitter Data

This vignette covers how to obtain and use Twitter API access tokens for use in the rtweet package.

# install from CRAN
install.packages("rtweet")

# load rtweet
library(rtweet)

Create Twitter App

creating

created

keys

NOTE: Following the steps outlined above, it’s possible to create multiple Twitter apps, resulting in multiple tokens. Twitter discourages abusing their API rate limits (these are like speed limits, regulating the amount of requests you can make within a given period of time). Abusing Twitter rate limits can even result in Twitter completely revoking your API access. However, using a small number of tokens seems to fall well below their threshold of concern.

Using Tokens with rtweet

twitter_tokens <- c(
  create_token(app = "rtweet_tokens", #whatever you named your app
    consumer_key = "XZgqotgOZNKlLFJqFbd8NjUtL",
    consumer_secret = "1rDnU3H3nrxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"), 
  create_token(app = "rtweet_roauth",
    consumer_key = "XZgqoxxxxxxxxxxxxxxxxxxxx",
    consumer_secret = "1rDnU3H3nrxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
  )
# (xxxxx's added but you get the point)

Saving Tokens

home_directory <- normalizePath("~/")
file_name <- paste0(home_directory, "/", "twitter_tokens")
save(twitter_tokens, file = file_name)

Environment Variable

cat("TWITTER_PAT=/Users/mwk/twitter_tokens\n", 
  file = paste0(home_directory, "/.Renviron"),
  append = TRUE)

That’s it!