Obtaining and using access tokens

knitr::opts_chunk$set(comment = "#>", collapse = TRUE)

##

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

  • To create a Twitter app, click here and decide on a Name, Description, and Website for your app (example screenshot provided below).
  • Important In the Callback URL field, make sure to enter the following: http://127.0.0.1:1410
  • Check yes if you agree and then click "Create your Twitter application".

creating

  • Once you've successfully created an app, click on the Keys and Access Tokens tab and copy down your consumer (api) and secret keys.

created

  • The keys you you'll need to create a token are the Consumer Key (aka API Key) and the Consumer Secret (aka API Secret).
  • I've highlighted the keys in the screen shot below.

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

  • With the access keys identified earlier, use create_token() and store the output as twitter_tokens.
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

  • At this point, you technically have enough to start using rtweet functions; you'd just need to set the token argument equal to twitter_tokens (the object created earlier).

  • Rather than creating tokens every time you open R, remembering where you saved your token(s) or passwords, and specifying a token every time you use a function, you can also store your tokens as an environment variable.

  • To do this, save the twitter_tokens object we created earlier in your home directory. To locate your home directory enter normalizePath("~/") into your R console (the home directory on my mac, for example, is "/Users/mwk/").

  • Now, save() the twitter_tokens object to the desired file location. I used the code below to as a shortcut to generate the file path ("/Users/mwk/twitter_tokens") for my tokens object.

home_directory <- normalizePath("~/") file_name <- paste0(home_directory, "/", "twitter_tokens") save(twitter_tokens, file = file_name)
  • Next you'll need to create an environment variable. This makes it easy to access your token(s) in future sessions (a best practice recommended by Hadley).

Environment Variable

  • Create a plain text file containing the path to your tokens object and save it to your home directory as ".Renviron".
  • To create a plain text file in R, modify the code below. Change the TWITTER_PAT location to match the path you used earlier (in the example below, you'd want to change "/Users/mwk/twitter_tokens"). You can also create a plain text document in any text editor like TextEdit or Notepad. If you're using Rstudio, select File > New File > Text File.

  • Important: Make sure the last line of ".Renviron" is blank. I achieved this in the code below by including \n.

cat("TWITTER_PAT=/Users/mwk/twitter_tokens\n", file = paste0(home_directory, "/.Renviron"), append = TRUE)
  • Restart R so it can process the environment variable.

  • If you completed the steps successfully, you should be able to use rtweet functions without specifying [or even thinking about] tokens [ever again]. If you have more than one token, rtweet will even quickly cycle through tokens to navigate API rate limits.

That's it!