knitr::opts_chunk$set(comment = "#>", collapse = TRUE)
This vignette covers how to obtain and use Twitter API
access tokens for use in the
# install from CRAN install.packages("rtweet") # load rtweet library(rtweet)
Create Twitter App
- To create a Twitter app, click here
and decide on a
Websitefor your app (example screenshot provided below).
- Important In the
Callback URLfield, make sure to enter the following:
- Check yes if you agree and then click "Create your Twitter application".
- Once you've successfully created an app, click on the
Keys and Access Tokenstab and copy down your consumer (api) and secret keys.
- 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.
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
- With the access keys identified earlier, use
create_token()and store the output as
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)
At this point, you technically have enough to start using
rtweetfunctions; you'd just need to set the
tokenargument 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_tokensobject 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/").
twitter_tokensobject 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).
- 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
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
rtweetfunctions 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.