5.12 Lab: Connect to Twitter Academic API

First we can check whether we have already registered a token:

(Please ask me for a token if you have trouble getting an account)

# Show environmental variable "TWITTER_BEARER"
Sys.getenv("TWITTER_BEARER")

If this is not the case we should do as described in the chapter describing how to use environment variables.

Start by opening the file. This will show you which environmental variables have been defined.

usethis::edit_r_environ(scope = "user")

If there is no line called TWITTER_BEARER=######, we would add the corresponding line (replace #### with our token) and save the file.

Then we can try to download some Twitter data using the API.

library(academictwitteR)
library(tidyverse)
library(lubridate)

# Count tweets
fff_ts <- count_all_tweets(query = "#fridaysforfuture lang:DE",
                           start_tweets = "2019-06-01T00:00:00Z",
                           end_tweets = "2020-05-31T00:00:00Z",
                           granularity = "day",
                           
                           n = 10)
head(fff_ts) # the data is in reverse chronological order


# Search for tweets containing PUTIN within time period
data <- get_all_tweets(query = "PUTIN", 
               start_tweets = "2022-03-14T00:00:00Z", 
               end_tweets = "2022-03-15T00:00:00Z", 
               bearer_token = get_bearer(), 
               data_path = "data",
               n = 10)
data$text