Reputation: 1
I have pretty basic knowledge in R. A few months ago, I used this simple code with the package Rtweet to retrieve tweets using my credentials of the Twitter API.
library(rtweet)
library(igraph)
library(tidyverse)
token <- create_token(
app = "name",
consumer_key = "XXXXXXXXXX",
consumer_secret = "XXXXXXXXXX",
access_token = "XXXXXXXXXX",
access_secret = "XXXXXXXXXX")
tweets <- search_tweets("search", n=50, type= "mixed", retryonratelimit = TRUE)
Since the API changes I cannot use this code anymore. When I try to run the code, I get a 403 error: “You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level.” I paid for the basic tier of the API and my app is attached to a project and I keep getting this error. Also, I tried resetting my tokens. What am I doing wrong?
Also, I tried another method of authentication, this function just asks me to type my bearer token:
auth <- rtweet_app()
tweets_libros <- search_tweets("libros sep", n=50, type= "mixed", retryonratelimit = TRUE, token = auth)
It gave me the same error.
Do you know if I am doing something wrong? Or just with the new changes the Twitter API just went nuts?
And, since I only know how to connect to the Twitter (or X) API with Rtweet, I asked Bard to give me a code to do so without the Rtweet package:
library(httr)
library(jsonlite)
# Set your Twitter API keys and secrets
consumer_key <- "XXXXXXXXXX"
consumer_secret <- "XXXXXXXXXX"
access_token <- "XXXXXXXXXX"
access_token_secret <- "XXXXXXXXXX"
# Create the authentication header
bearer_token <- paste0("Bearer ", access_token)
headers <- c(Authorization = bearer_token)
# Search for tweets about "rstats" and limit the results to 10 tweets
url <- "https://api.twitter.com/2/tweets/search/recent?q=rstats&n=10"
response <- httr::GET(url, headers = headers)
# Check the response status code
if (response$status_code == 200) {
# The request was successful
tweets <- jsonlite::fromJSON(content(response, "text"))
print(tweets)
} else {
# The request failed
print(paste("Error:", response$status_code))
}
I don't know how the code above works, but i tried replacing "bearer" with my bearer token and still this does not works. When I run the code above it returns me the 401 error.
Upvotes: 0
Views: 480
Reputation: 3397
You paid for the API access via OAuth 2.0 but you don't use it. That's why you keep getting the same error.
Read the authentication vignette to use the Oauth 2.0 , which basically says to use rtweet_client
and rtweet_oauth2
to obtain the authentications.
Once you get the token you'll need to use tweet_search_recent
or tweet_search_all
because these functions are the ones that use the API v2.
Note that I would recommend to install the httr2 devel version and the rtweet from github (from the devel branch) if you want a painless experience with the authentication.
Upvotes: 0