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

403 error although authentication seemingly working #729

Closed mgei closed 2 years ago

mgei commented 2 years ago

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

bearer_token <- "..."

library(rtweet)

auth <- rtweet_app(bearer_token = bearer_token)

lookup_tweets("1558680061884043265", token = auth)
# Error: Twitter API failed [403]
# Check error message at https://developer.twitter.com/en/support/twitter-api/error-troubleshooting

search_tweets("#rstats", token = auth)
# Error: Twitter API failed [403]
# Check error message at https://developer.twitter.com/en/support/twitter-api/error-troubleshooting

The following works, so I assume that there's no problem with the authentication.

require(httr)
require(jsonlite)

bearer_token
headers <- c(Authorization = sprintf('Bearer %s', bearer_token))

params <- list(tweet.fields = "text",
               ids = "1558680061884043265")

url <- "https://api.twitter.com/2/tweets"

response <- httr::GET(url = url,
                      httr::add_headers(.headers = headers),
                      query = params)

obj <- httr::content(response, as = "text")

parse_json(obj, simplifyVector = T)$data$text
# [1] "What is your best investing advice?"

Same when I try bot authentication.

Aariq commented 2 years ago

I'm getting this too. I thought maybe it was because I was on a conference wifi or something??

mgei commented 2 years ago

I had to apply for "Elevated" https://developer.twitter.com/en/portal/products/elevated This required me to fill in some fields for intended use etc. and after that it immediately worked.

llrs commented 2 years ago

Note that on the second code you use twitter API v2 that rtweet doesn't use yet.

Your link goes to the API v2 but I think this is also needed for the API v1.1 which rtweet uses. I don't recall where to find information about using bearer tokens with Twitter API v1.1 but as you found out it might be needed to request elevated access for some endpoints.

Glad you sorted it out and you reported it here. Thanks Martin.