Twitter API Failed [403]

Here’s my code:

```{r setup, include=FALSE}
require(httr)
require(jsonlite)
require(dplyr)
require(magrittr)
require(tidyverse)
library(devtools)
library(rtweet)


## store api keys (these are fake example values; replace with your own keys)
app_name <- "TweeterJasenHicks"
api_key <- "***"
api_secret_key <- "***"
AccessToken <- "***"
AccessTokenSecret <- "***"

create_token(
app = app_name,
consumer_key = api_key,
consumer_secret = api_secret_key,
access_token = AccessToken,
access_secret = AccessTokenSecret
)

auth <- rtweet_app()
auth_save(auth, "JasenTwitterAccess")


auth_as("JasenTwitterAccess")

df <- search_tweets("#MiSTerFPGA", token = auth)

Everything works up until the point where I create the dataframe df, it throws the error:
Twitter API Failed [403]

Not sure why. Its authenticating and everything seems to be setup correctly as it prompts me for the bearer token, I put it in and it continues on well. But as soon as the df line is ran… error in the console.

You seem to be the first user of rtweet 1.0.2. Congratulations!

It is not clear to me if you stored the token you create in a variable. Did you do something like this?

token <- create_token(app = app_name,
                      consumer_key = api_key,
                      consumer_secret = api_secret_key,
                      access_token = AccessToken,
                      access_secret = AccessTokenSecret)
auth_save(token, "JasenTwitterAccess")

Could you post auth_sitrep() output? This will help detect and move some old tokens and provide information about what is the state of authentication on your computer.

Last could you try:

df <- search_tweets("#MiSTerFPGA")

If you used auth_as there is no longer need to provide a token to each requests, as it will automatically use the one you said on the session. In this case if auth is empty (and you didn’t follow the prompts you got with rtweet_app it will make your requests to fail. )

The error message 403 should mean “it has been refused or access is not allowed. An accompanying error message will explain why.” (from Twitter help page). Did you see any other message?

1 Like

If I am the first… just happenstance. I am learning R as part of my MBA and I love it. I also love messing with data and seeing how I can analyze social media more efficiently. Today is my first foray into the Twitter + R world.

Output is:

Error in `.rowNamesDF<-`(x, value = value) : 
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique value when setting 'row.names': ‘list(token = "888")’ 

888 was my BearerToken, removed for security.

No other errors in the console to help figure it out. But I’m sure I am doing something wrong… the code posted is all of my code so far.

I updated the code to this:


```{r setup, include=FALSE}
require(httr)
require(jsonlite)
require(dplyr)
require(magrittr)
require(tidyverse)
library(devtools)
library(rtweet)


## store api keys (these are fake example values; replace with your own keys)
app_name <- "TweeterJasenHicks"
api_key <- "***"
api_secret_key <- "***"
AccessToken <- "***"
AccessTokenSecret <- "***"

token <- create_token(
app = app_name,
consumer_key = api_key,
consumer_secret = api_secret_key,
access_token = AccessToken,
access_secret = AccessTokenSecret
)

auth_save(token, "JasenTwitterAccess")
## authenticate via web browser

auth_as("JasenTwitterAccess")

df <- search_tweets("#MiSTerFPGA", token = auth)

It spit out the same error. The updated auth_sitrep() outputs though did change:

Tokens found on C:/Users/jasen/AppData/Roaming/R/config/R/rtweet:
          token
some-name     A
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
create_token       rtweet            B
default            rtweet 30514805   A
JasenTwitterAccess rtweet            B

When I used

> df <- search_tweets("#MiSTerFPGA")

It spit out:

Warning: Could not authenticate you. (32)
Error: Twitter API failed [401]
Check error message at https://developer.twitter.com/en/support/twitter-api/error-troubleshooting

Glad you are enjoying learning R. I’m sorry but I don’t understand where does the first error come from. It seems it could be from auth_sitrep(), but I’m not sure. (Well done to remove the Bearer Token from the output).

The output of auth_sitrep below the code is very informative. You have 3 tokens saved, for the same app, two of them with the same key. Could you provide the output of auth_get()

Use auth_as("default") and try again:

library("rtweet")
auth_as("default")
df <- search_tweets("#MiSTerFPGA")
1 Like

Whelp, call me a monkey’s uncle… that worked. It only retrieved 100 tweets. But now I’m playing with fire!

1 Like

Glad it works now. I wish you a successful master and happy learning :smiley:

1 Like