Authorization methods
Users can create their personal Twitter token in two different ways. Each method is outlined below.
1. Browser-based authentication
- Authentication via web browser requires the
httpuv
package to be installed.
- Navigate to developer.twitter.com/en/apps and select your Twitter app
- Click the tab labeled
Keys and tokens
to retrieve your keys.
- Locate the
Consumer API keys
(aka “API Secret”).

- Copy and paste the two keys (along with the name of your app) into an R script file and pass them along to
create_token()
.
## load rtweet
library(rtweet)
## store api keys (these are fake example values; replace with your own keys)
api_key <- "afYS4vbIlPAj096E60c4W1fiK"
api_secret_key <- "bI91kqnqFoNCrZFbsjAWHD4gJ91LQAhdCJXCj3yscfuULtNkuu"
## authenticate via web browser
token <- create_token(
app = "rstatsjournalismresearch",
consumer_key = api_key,
consumer_secret = api_secret_key)
- A browser pop-up window should appear. Click to approve (must be signed into twitter.com) and return to R.
- Your token has been created. Print your token to make sure the app name and
api_key
match
## view token (you should see the correct app name)
token
2. Access token/secret method
- Navigate to developer.twitter.com/en/apps and select your Twitter app
- Click the tab labeled
Keys and tokens
to retrieve your keys.
- Locate the
Consumer API keys
(aka “API Secret”).

- Scroll down to
Access token & access token secret
and click Create

- Copy and paste the four keys (along with the name of your app) into an R script file and pass them along to
create_token()
.
## 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 = "rstatsjournalismresearch",
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.
That’s it!