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

Authentication failing with `auth_setup_default()` #756

Closed ccamara closed 1 year ago

ccamara commented 1 year ago

Problem

I just installed rtweet v 1.1.0 in a new machine and I cannot authenticate. When I run auth_setup_default() I get the following error:

Error in httr::init_oauth1.0(endpoint, app, permission = permission, is_interactive = is_interactive, : Bad Request (HTTP 400).

Expected behavior

Rtweet should authenticate using my twitter credentials from my logged account in default web browser.

Reproduce the problem

  1. Install latest rtweet version (install.packages("rtweet", repos = 'https://ropensci.r-universe.dev'))
  2. Authenticate running rtweet::auth_setup_default()

rtweet version

## copy/paste output
packageVersion("rtweet")

‘1.1.0.9000’

Session info

## copy/paste output
> sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9          withr_2.5.0         digest_0.6.31       later_1.3.0         R6_2.5.1            magrittr_2.0.3     
 [7] httr_1.4.4          rlang_1.0.6         cli_3.6.0           curl_5.0.0          rstudioapi_0.14     promises_1.2.0.1   
[13] rtweet_1.1.0.9000   xaringanExtra_0.7.0 tools_4.2.2         htmlwidgets_1.6.1   httpuv_1.6.8        xfun_0.36          
[19] fastmap_1.1.0       compiler_4.2.2      askpass_1.1         htmltools_0.5.4     openssl_2.0.5       knitr_1.41         
> 
ccamara commented 1 year ago

Not sure if this is an issue related to the package or the API itself, as it has been reported to fail in some cases (https://www.theverge.com/2023/1/13/23553161/third-party-twitter-clients-apps-outage-twitterific-tweetbot).

llrs commented 1 year ago

I have noticed some problems with some endpoints lately, but a couple of days ago I could authenticate without problems. I'll try again and let you know, but I think this is a problem from Twitter not the package.

But I have been experiencing some problems with the authentication mechanism, so I will be monitoring/checking them more closely for a while. Thanks for reporting it.

llrs commented 1 year ago

For anyone with this same problem: Some users have reported that they could authenticate without problems via rtweet_app after obtaining their own credentials. This requires you, the user, to ask for developer access in order to obtain the bearer token but your credentials won't be tied to rtweet tokens.

macmac4528 commented 1 year ago

Hi, ive been having this same problem (Bad Request HTTP 400). I just got developer access (essential level) and still have the same problem. I've even ran the rtweet_app() code and entered my bearer token.

Are there any additional steps to authenticating the twitter account in R other than running auth_setup_default()? If so, what are the R codes needed?

Sorry i am very new to pulling twitter data and R in general as I am a student in an analytics class. Your help would be greatly appreciated!

llrs commented 1 year ago

@macmac4528 Sorry to hear you have problems with rtweet. However, this is not the place to ask for help as this is to report if there are bugs with the package (which from your comment I don't think there is any).

If you have questions, please ask at the rOpenSci forum with exactly the code run in the same order (this is known as reproducible example and there are helpers to get it done such as reprex), it will help me help you. I'm not sure if you have read the authentication vignette but sharing what you tried (as you did here) and what have you read will help too. Good luck!

llrs commented 1 year ago

Mmh, it seems that while working in #744 I introduced an error that slipped through the checks into the release. Apologies! This should now be fixed in devel in version 1.1.0.9001: install.packages("rtweet", repos = 'https://ropensci.r-universe.dev/') or remotes::install_github("ropensci/rtweet"). I'll try to make a new release soon.

ratnanil commented 1 year ago

https://github.com/ropensci/rtweet/issues/756#issuecomment-1401069647 solved the issue for me!

llrs commented 1 year ago

Thanks for confirming it @ratnanil. I'll close the issue now (but I'll pin it for users of current release)

flxmrtn commented 1 year ago

"> Mmh, it seems that while working in #744 I introduced an error that slipped through the checks into the release. Apologies!

This should now be fixed in devel in version 1.1.0.9001: install.packages("rtweet", repos = 'https://ropensci.r-universe.dev/') or remotes::install_github("ropensci/rtweet"). I'll try to make a new release soon."

Hello, if I do this, it works for one time. Afterward, I have to install the package again, restart Rstudio, and repeat it for the next Account. With the remotes::install it works without restarting, but I still have to repeat the installation. As I want to pull tweets from 1500 accounts, I would like to do it with a for loop, but that is not working for me with installing the package inside the loop. That's my loop, maybe someone can help me.

` big_tml = list() tml = data.frame() user = vector()

for (i in length(df$screen_name)){ tml<-get_timeline(df$scree_name[i], n=500, auth_setup_default()) user<-as.character(df$scree_name[i])

tml<- cbind(tml,user) big_tml<-rbind(big_tml,tml) }`

flxmrtn commented 1 year ago

Sorry I was to fast, thats my first time. following, my try of a loop with including installation and authentication etc. which does not solve the problem for me.

`for (i in 1:length(df$screen_name2)){

remotes::install_github("ropensci/rtweet", force = TRUE) library("rtweet", "twitteR", "httr") df <- read.csv("Data_politicians.csv", sep = ";") df <- dplyr::select(politicians,c("lastname", "firstname", "gender", "state", "id", "party", "screen_name1", "user_id1")) df <- df[-which(is.na(df$user_id1)),] df$screen_name2 <- paste0("@",df$screen_name1)

consumer_key <- "xxx" consumer_secret <- "xxx" access_token<- "xx-xx" access_secret <- "xx"

setup_twitter_oauth(consumer_key ,consumer_secret, access_token, access_secret)

tml<-get_timeline(df$scree_name[i], n=500, auth_setup_default())

user<-as.character(df$scree_name[i])

tml<- cbind(tml,user) big_tml<-rbind(big_tml,tml) }`

llrs commented 1 year ago

@flxmrtn If you want to reuse a token you need to use rtweet::auth_save to save the token and auth_as to reuse them later on.

You seem to use multiple packages. Please post your problem with the package to the original packge causing your issue or post your question in a public site if you have a question about how to use something.

TCSMA commented 1 year ago

The Authentication using auth_setup_default() is not working. After I installed the retweet using the development version, there is another error message when I run auth_setup_default(). Error in httr::init_oauth1.0(endpoint, app, permission = permission, is_interactive = is_interactive, : Unauthorized (HTTP 401). Are there any ways to solve this problem? Thank you very much.

llrs commented 1 year ago

@TCSMA The error message seems to indicate you didn't authorize rtweet to access you account (401 Unauthorized). Maybe you weren't logged in Twitter. Are you sure there is a problem with rtweet?

Did you in the browser get redirected to something like http://127.0.0.1:1410/?oauth_token=m_qADFFDDSfdfdsafABhyosmoA&oauth_verifier=nLdfvDypho6Bafdasfd5WgfKdnbK0ixKSG?

TCSMA commented 1 year ago

Thank you very much for your reply. I don't see any window that pops up for authentication. When I first install the rtweet in the development version, there was error about dependency and failed to install it. Then I install rtweet using the published version. Then I install the development version again. Then I got the error message.

llrs commented 1 year ago

Can you provide the output of auth_sitrep()?

TCSMA commented 1 year ago

When I first installed the development version of R, I cannot install it and got the following error message. This step worked on Monday afternoon and stopped working on Tuesday morning.

install.packages("rtweet", repos = 'https://ropensci.r-universe.dev/') WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/ Installing package into ‘C:/Users/CBS-HELAB2-09/Documents/R/win-library/4.1’ (as ‘lib’ is unspecified) Warning in install.packages : dependencies ‘bit64’, ‘curl’, ‘httr’, ‘httr2’, ‘jsonlite’, ‘lifecycle’, ‘progress’, ‘rlang’, ‘tibble’, ‘withr’ are not available

There is a binary version available but the source version is later: binary source needs_compilation rtweet 1.1.0.9001 1.1.0.9011 FALSE

installing the source package ‘rtweet’

trying URL 'https://ropensci.r-universe.dev/src/contrib/rtweet_1.1.0.9011.tar.gz' Content type 'application/x-gzip' length 3349301 bytes (3.2 MB) downloaded 3.2 MB

ERROR: dependencies 'bit64', 'curl', 'httr', 'httr2', 'jsonlite', 'lifecycle', 'progress', 'rlang', 'tibble', 'withr' are not available for package 'rtweet'

Then I installed the published version of rtweet, the installation is successful but cannot use auth_setup_default(). The error message is Error in httr::init_oauth1.0(endpoint, app, permission = permission, is_interactive = is_interactive, : Bad Request (HTTP 400).

Then I installed the development version of rtweet again. When I run auth_setup_default(), the error message is

auth_setup_default() Error in httr::init_oauth1.0(endpoint, app, permission = permission, is_interactive = is_interactive, : Unauthorized (HTTP 401). auth_sitrep()

There is no output if I use auth_sitrep() Thank you so much!

llrs commented 1 year ago

When you tried to install from r-universe it told you that the binaries are outdated. When trying to install from source you can't because you don't have Rtools installed. To install RTools you'll need to know the R version and install it from its website. The problem with the binaries is that r-universe only provides them for R 4.3 and R 4.2 not R 4.1 which seem to be the version you are running.

It seems that you don't have loaded rtweet and so auth_sitrep didn't work. Try again with rtweet::auth_sitrep() or first loading the package: library("rtweet").

So far I haven't seen any evidence that rtweet is failing with rtweet version 1.1.0.9001 or 1.1.0.9011. Please let me know when you have some problems with version 1.1.0.9011 or later. Thanks!

TCSMA commented 1 year ago

Thanks a lot! I installed the RTools and reinstall rtweet development version. I successfully installed the version but I still get the same error. It seems that rtweet_user() function returns this error. Error in httr::init_oauth1.0(endpoint, app, permission = permission, is_interactive = is_interactive, : Unauthorized (HTTP 401).

llrs commented 1 year ago

@TCSMA Please provide the rtweet version you are using and the output of rtweet::auth_sitrep(), also please provide the full message you get in your terminal and the traceback() after the error. Without it I can only make wild guesses.

TCSMA commented 1 year ago

Thank you very much for helping. The rtweet version is rtweet_1.1.0.9011 auth_sitrep() and rtweet::auth_sitrep() returned nothing.

auth_sitrep() rtweet::auth_sitrep() traceback() 9: stop(http_condition(x, "error", task = task, call = call)) 8: stop_for_status(response) 7: httr::init_oauth1.0(endpoint, app, permission = permission, is_interactive = is_interactive, private_key = private_key) 6: twitter_init_oauth1.0(self$endpoint, self$app, permission = self$params$permission, private_key = self$private_key) 5: self$init_credentials() 4: initialize(...) 3: TwitterToken1.0$new(app = app, endpoint = httr::oauth_endpoints("twitter"), params = list(as_header = TRUE), cache_path = FALSE) 2: rtweet_user() 1: auth_setup_default()

llrs commented 1 year ago

Sorry, this can be the new Twitter policy regarding the API or something else (that you need elevated access or something). You will have to debug this issue further for me to see if I need to fix something.

azadesel commented 1 year ago

I am also having the same problem. I started to get "Error in httr::init_oauth1.0(endpoint, app, permission = permission, is_interactive = is_interactive, : Bad Request (HTTP 400)." and then try to go around it with rtweet_app() because I have academic twitter access, I am entering my bearer token and still getting the same message when I run auth_setup_default() . I tried to use regenerated tokens or use other tokens for other apps but now I am getting error 401 unauthorized. When I run auth_sitrep() it is giving

Tokens from rtweet version < 1.0.0 found on /Users/_: Multiple authentications with the same key found! Multiple authentications with the same app found! Choose which is the best path of action for the tokens: app user_id key .rtweet_token Automated Twitter SNA A .rtweet_token1 Automated Twitter SNA A .rtweet_token2 Automated Twitter SNA A .rtweettoken3 Automated Twitter SNA A All tokens should be moved to /Users//Library/Preferences/org.R-project.R/R/rtweet

and now every time I try auth_setup_default() it is giving me 401 unauthorization error not the 400.

I tried to run it on another pc which I didn't try any tokens etc it also does not work. I am doing this on mac R 4.1 and for PC R 4.2 .

llrs commented 1 year ago

@azadesel You have duplicated tokens but don't seem to have saved bearer tokens (from your academic access) or the default token. If you set up your academic access via the bearer token you don't need to run auth_setup_default().

You also seem to be using rtweet 1.1.0 with the bug that started this issue. Please install the devel version from github where this is fixed.

azadesel commented 1 year ago

Yes, I uploaded to the latest one (with remotes::install_github("ropensci/rtweet")), still getting the same error.

"If you set up your academic access via the bearer token you don't need to run auth_setup_default()." also yes but even though when I enter my bearer and got "Twitter bearer token" on the console when i try to search tweets it is giving me this error. "Error in default_cached_auth(): ! No default authentication found. Please call auth_setup_default() Run rlang::last_trace() to see where the error occurred." after I wrote rtweet_app() should I do something else for authentication

llrs commented 1 year ago

Ok, @azadesel it seem you introduce your token but you don't save or use it for each function. Please carefully read the vignette about authorization, for easier access you should:

library("rtweet")
token <- rtwet_app()
auth_as(token)
auth_save(token, "academic_token")
su <- search_users("#rstats", n = 200)

In a fresh session then you can do this:

library("rtweet")
auth_as(token, "academic_token")
su <- search_users("#rstats", n = 200)
TCSMA commented 1 year ago

@llrs . Thank you very much for your help. I also saw the developer portal interface changed as well.

llrs commented 1 year ago

Twitter banned the default app used, reported by the original maintainer:

Add rtweet to the list. The app it uses under the hood was suspended last night as part of @TwitterDev deprecation of free APIs. 😭 https://twitter.com/MattBinder/status/1643301705243582486 image

I won't provide new default authentications (beyond what is in rtweet 1.2). All users will need to setup their own credentials (or reuse the few that still work).