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.
::edit_r_environ(scope = "user") usethis
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
<- count_all_tweets(query = "#fridaysforfuture lang:DE",
fff_ts 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
<- get_all_tweets(query = "PUTIN",
data start_tweets = "2022-03-14T00:00:00Z",
end_tweets = "2022-03-15T00:00:00Z",
bearer_token = get_bearer(),
data_path = "data",
n = 10)
$text data