Skip to contents

Overview

Apply for a developer account

  • You must have a Twitter account
  • Apply for a developer account:
    • https://developer.twitter.com/en
    • https://developer.twitter.com/en/portal/petition/use-case
      • Choose ‘Student’
      • Fill out the form on:
        • how you will use the Twitter API/ Data?1
        • Specifics:
          • Are you planning to analyze Twitter data? Yes. Add general description.2
          • Will your app use Tweet, Retweet, Like, Follow, or Direct Message functionality? No.
          • Do you plan to display Tweets or aggregate data about Twitter content outside Twitter? Yes. Describe that the data will be used for a research project (details if you like) and that it will be aggregated (you will not share full tweet content outside of Twitter)3
          • Will your product, service, or analysis make Twitter content or derived information available to a government entity? No.
      • Submit application and verify your email

Create a project

  • Create a project https://developer.twitter.com/en/portal/dashboard
    • Name the project (Example “Language use”)
    • Use case: “Student”
    • Project description: Use the description for “Are you planning to analyze Twitter data?” in the Developer application.
    • App name: give it the same name as your GitHub Project, “project_”, so mine is “project_francom”.
    • Copy the API Key, API Key Secret, and Bearer Token and store them in a safe and accessible place (you’ll need them later.)
    • Enable 3-legged OAuth
      • Click ‘Edit’
        • Enter “CALLBACK URLS” as http://127.0.0.1:1410
        • Enter “WEBSITE URL” as your twitter profile page (e.g. https://twitter.com/jrad_cole)

Authenticate with R

Note you will need to complete the following steps on a local machine. You cannot create an authentication token in RStudio Cloud. Refer to this guide to install R and RStudio on your own computer.

  • Open an .R script and save it as _twitter_auth.R

Only if you plan on pushing this project to GitHub:

  • Edit your .gitignore
    • add _twitter_auth.R to the list

Continue with the following steps:

  • Return to the _twitter_auth.R file
    • Load the rtweet package
## stored api keys 
# (these are fake example values; replace with your own keys)
app_name <- "project_francom"
api_key <- "afYS4vbIlPAj096E60c4W1fiK"
api_secret_key <- "bI91kqnqFoNCrZFbsjAWHD4gJ91LQAhdCJXCj3yscfuULtNkuu"

## authenticate via web browser
token <- create_token(
  app = app_name,
  consumer_key = api_key,
  consumer_secret = api_secret_key)

You will now have a token saved in your R Project environment for use.

If you want to use this token on RStudio Cloud, you will need to first save token as an .rds file.

saveRDS(token, file = "~/Desktop/token.rds")

Then you will upload it to the RStudio Cloud project where you want to interface with the Twitter API.

To use the token, read the token.rds file and assign it to an object.

token = readRDS("token.rds")

Use this object name (token) in your calls to rtweet functions (token = token).

One final note, if you plan to push your project to GitHub include the name of the .rds token file in .gitignore to that it is not made public.


  1. Example: “I plan to search for tweets matching certain terms and then analyze the language that is associated with these terms. This will be part of a research project for a university course on text analysis methods.”↩︎

  2. Example: “I plan to search for tweets matching certain terms and then analyze the language that is associated with these terms. The goal will be to understand how certain terms are related to types of linguistic discourse and whether there is a relationship between these terms and geographic location. This aim is to understand sociolinguistic trends in language use.”↩︎

  3. Example: “The analysis of the language returned from term searches will be analyzed and shared only in aggregate form as part of a research project on language use and variation.”↩︎