ropensci / rtweet

🐦 R client for interacting with Twitter's [stream and REST] APIs
https://docs.ropensci.org/rtweet
Other
785 stars 201 forks source link

I get a 403 error message after authenticating using the rtweet_app() function #722

Closed pedrotercero3 closed 2 years ago

pedrotercero3 commented 2 years ago

Problem

I get the following 403 error message while trying to autheticate an app using the rtweet_app() function and following the official documentation:

Error: Twitter API failed [403]
Check error message at https://developer.twitter.com/en/support/twitter-api/error-troubleshooting

Expected behavior

Well, the expected behavior is to get the first 100 tweets according to the search criterion in the code below. I created a new app in the Twitter Developer account as especified in the documentation. I tried resetting the API keys, creating a new app again... nothing works. I tried inputting the bearer token both as shown in the code below and also using the rtweet_app() function as-is (which shows a new window where you can enter the token), getting the same error.

Reproduce the problem

Code:

# Using app based authentication
library(rtweet)

auth <- rtweet_app(twitter_keys$value[twitter_keys$variable == "bearer_token"])

auth_as(auth)

# Testing if it works
test <- search_tweets("#rstats", n = 100)

Output:

Error: Twitter API failed [403]
Check error message at https://developer.twitter.com/en/support/twitter-api/error-troubleshooting

rtweet version

## copy/paste output
packageVersion("rtweet")
‘1.0.2’

Session info

## copy/paste output
sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Spain.utf8  LC_CTYPE=Spanish_Spain.utf8   
[3] LC_MONETARY=Spanish_Spain.utf8 LC_NUMERIC=C                  
[5] LC_TIME=Spanish_Spain.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] rtweet_1.0.2    forcats_0.5.1   stringr_1.4.0  
 [4] dplyr_1.0.9     purrr_0.3.4     readr_2.1.2    
 [7] tidyr_1.2.0     tibble_3.1.8    ggplot2_3.3.6  
[10] tidyverse_1.3.2

loaded via a namespace (and not attached):
 [1] progress_1.2.2      tidyselect_1.1.2    xfun_0.31          
 [4] haven_2.5.0         gargle_1.2.0        colorspace_2.0-3   
 [7] vctrs_0.4.1         generics_0.1.3      utf8_1.2.2         
[10] rlang_1.0.4         pillar_1.8.0        glue_1.6.2         
[13] withr_2.5.0         DBI_1.1.3           bit64_4.0.5        
[16] dbplyr_2.2.1        modelr_0.1.8        readxl_1.4.0       
[19] lifecycle_1.0.1     munsell_0.5.0       gtable_0.3.0       
[22] cellranger_1.1.0    rvest_1.0.2         knitr_1.39         
[25] tzdb_0.3.0          curl_4.3.2          parallel_4.2.1     
[28] fansi_1.0.3         broom_1.0.0         scales_1.2.0       
[31] backports_1.4.1     googlesheets4_1.0.0 vroom_1.5.7        
[34] jsonlite_1.8.0      fs_1.5.2            bit_4.0.4          
[37] hms_1.1.1           stringi_1.7.8       grid_4.2.1         
[40] cli_3.3.0           tools_4.2.1         magrittr_2.0.3     
[43] crayon_1.5.1        pkgconfig_2.0.3     ellipsis_0.3.2     
[46] prettyunits_1.1.1   xml2_1.3.3          reprex_2.0.1       
[49] googledrive_2.0.0   lubridate_1.8.0     assertthat_0.2.1   
[52] httr_1.4.3          rstudioapi_0.13     R6_2.5.1           
[55] compiler_4.2.1
llrs commented 2 years ago

Hi, what is this twitter_keys object you show here? It seems a data.frame with some keys and data stored. But if there are several "bearer_token" this approach could provide several bearer tokens.

When you add the bearer token via the pop-up window, are you sure that the bearer token is still working and hasn't been updated or that you copied it all of it? If you tried several times it could be that there is some delay between approval and the API recognizing the result.

Could you show the result of auth_sitrep()? This would help me detect if there is any problem with other saved authentications

pedrotercero3 commented 2 years ago

Hi @llrs, and thanks for the quick reply. You're right, I've stored the keys in a data frame, but there's only one bearer token, so there's no room for mistakes. I tried authenticating several times via the pop-up window ensuring that I was copying it correctly and it still failed. I also waited for several hours between tries just in case there was a delay, as you said.

Here's the output of auth_sitrep():

Tokens found on C:/Users/pedrotercero/AppData/Roaming/R/config/R/rtweet:
         token
app-auth     A

I also tried using auth_as(), and the token argument directly in the search_tweets() function, but nothing works.

llrs commented 2 years ago

Thanks for confirming this is indeed the right token. Probably you need authorization from Twitter or you have already used all your requests with that token (check documentation).

I don't think this is a bug from rtweet, as there isn't any warning or error on R side but the problem is on what Twitter returns. Closing this now, unless you find something that shows that it is a problem in rtweet (in that case I would reopen the issue to fix it).