aterhorst
aterhorst

Reputation: 685

rtweet - Warning: 32 - Could not authenticate you

I notice a few people have encountered issues with rtweet authorization. I followed this vignette which assumes people are familiar with authentication protocols. Newbies such as myself, struggle with this.

vignette("auth", package = "rtweet")`

I stored all keys and tokens in my .Renviron file. This is the code I use to access keys and tokens:

require(rtweet)
require(httpuv)

api_key <- Sys.getenv("TWITTER")
api_secret_key <- Sys.getenv("TWITTER_SECRET")
access_token <- Sys.getenv("ACCESS_TOKEN")
access_token_secret <- Sys.getenv("ACCESS_SECRET")

I then create a token using this code:

token <- create_token(
  app = "my_app",
  consumer_key = api_key,
  consumer_secret = api_secret_key,
  access_token = access_token,
  access_secret = access_token_secret)

Next, I check if a token has been created:

get_token()

I get this response:

<Token>
<oauth_endpoint>
 request:   https://api.twitter.com/oauth/request_token
 authorize: https://api.twitter.com/oauth/authenticate
 access:    https://api.twitter.com/oauth/access_token
<oauth_app> my_app
  key:    XXXXXXXXXXXXXXXXXXXX
  secret: <hidden>
<credentials> oauth_token, oauth_token_secret
---

I restarted R and run a query as follows:

tweet_data <- search_tweets("#plasticrecycling", 
                 n = 2000, 
                 include_rts = F, 
                 lang = "en")

And end up with this error:

Warning: 32 - Could not authenticate you.
Warning message:
Could not authenticate you. 

Can someone please explain why this is happening? A similar issue was reported here https://github.com/ropensci/rtweet/issues/439 but it was closed without a clear answer. I am not sure if create_token() is a once-off thing or must be initiated each time I use R. I notice this added to my .Renviron file:

TWITTER_PAT=/Users/bob/.rtweet_token.rds

Thanks in advance.

Upvotes: 1

Views: 776

Answers (1)

aterhorst
aterhorst

Reputation: 685

Loading the latest development version of rtweet solves the problem I was encountering:

remotes::install_github("rOpenSci/rtweet")

packageVersion("rtweet")
[1] ‘0.7.0.9026’

This version makes it easier to authenticate things.

auth <- rtweet_app(bearer_token = Sys.getenv("BEARER_TOKEN"))

auth_save(auth, "my_app")

auth_list()
[1] "my_app"

auth_as(auth = "my_app")

tweets <- search_tweets("#plasticrecycling",
                        include_rts = F,
                        n = 1000,
                        lang = "en")

One only has to run the rtweet_app() once and then save the output. Every time one restarts R and loads a Twitter script, one simply has to execute auth_as(auth = "your_app")

I was confused by OAuth1.0 versus OAuth2.0 on the Twitter API page. It also did not help reading the wrong online manual pages for a different version of rtweet!

Kudos to llrs for helping me resolve my issue.

Upvotes: 1

Related Questions