-1

I am doing a basic Twitter sentiment analysis in RStudio: I already have a Twitter developer account.

Below is the code for getting the tweets:


title: "Twitter" output: html_document

knitr::opts_chunk$set(echo = TRUE)
library(rtweet)  # Twitter package
# locations for analysis
mex_loc <- c(mexico_city = "19.43,-99.13,50mi",
             guadalajara = "20.67,-103.35,50mi",
             monterrey   = "25.67,-100.3,50mi",
             toluca      = "19.28,-99.66,50mi")
# extract tweets
myquery <- "add my query here" 

mytweets <- list()

# To use app based authentication
auth <- rtweet_app()

for (i in 1:4) {
  mytweets[[i]] <- search_tweets(q = myquery,
                              n = 18000,
                              lang = "es",
                              geocode = mex_loc[i],
                              until = '2023-03-22' ,
                              include_rts = FALSE, 
                              retryonratelimit = FALSE)
}

tweet_dt_retail <- data.table::rbindlist(mytweets, use.names=TRUE )

However whe I extract the tweets I get this error:

![Error when extract tweets] (https://i.stack.imgur.com/haA9R.png)

I would appreciate your help,

Javier

Javier
  • 31
  • 3

1 Answers1

0

When I first authenticate via access token the error is solved (the keys and secrets are gotten once I setup a Twitter developer account). Please find below the code:

## authenticate via access token
token <- create_token(
  app = "my app",
  consumer_key = "my key",
  consumer_secret = "my secret",
  access_token = "my token",
  access_secret = "my token secret")

# save my token
saveRDS(token,"myTwitterToken")
# get the token
token <- readRDS("myTwitterToken")

Once I did that I can run this code without an error

Javier
  • 31
  • 3