rtweet authorization

Greetings. I notice a few people have asked on StackOverflow for help on rtweet authorization. None have been answered. 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 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 and closed without any decent explanation. This is a problem experienced by others and deserves a decent answer. I am not sure if this 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.

Thanks for reporting that there are unanswered questions on Stackoverflow, I do not monitor SO rtweet tag. But I will try to help there too.

Is there anything that would help you? What do you miss on the vignette? What were your struggles?

For the next version of rtweet there have been a lot of changes on the authorization vignette. Do you find this document Authentication with rtweet • rtweet more helpful ? (note that it is for the next release of rtweet so it doesn’t apply to your version of rtweet).

Did you use the latest secrets and tokens? A common problem is regenerating them and not updating them in the .Renviron

I closed the issue 'Could not authenticate you' when using search_tweets function · Issue #439 · ropensci/rtweet · GitHub because the question was old and I think it was unlikely that user would provide more information needed in order to help them.

I am sure all of us would like to get more than we get.

Yes, creating tokens on rtweet 0.7.0 adds a line to the .Rprofile
You only need to store your authentication once (and renew the tokens if you regenerate them via your Twitter developer website)

Do you have other files on your home directory that start with .rtweet_token ? They may be .rtweet_token.1rds or something similar. If there are you should delete the old authentications and keep the one with the higher number (and edit your .Rprofile to point to that token).

I hope this helps

I loaded the latest developer version:

devtools::install_github("mkearney/rtweet")

And then followed https://docs.ropensci.org/rtweet/articles/auth.html. This code seems to have worked:

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

tweets <- search_tweets("#advancedrecycling",
                        include_rts = T,
                        token = auth,
                        n = 2000,
                        lang = "en")

When I queried ??rtweet_app the help file had this usage example: rtweet_app(bearer_token = ask_pass("bearer token"))``. This returned an error there is no ask_pass` function.

Glad you find a way to work with rtweet. Could you please answers to the question I made on the first post? Specially about what did you find confusing about the authorization vignette and if this is more clear now.

Could you format correctly the last answer? I’m not sure I understand where was this error visible. Is it in the help page or when you tried running the examples of the help page?

Thank you. The first vignette does not differentiate between OAuth 1.0 and OAuth 2.0. I am not sure if this authentication is a once off requirement or must be rerun each time I reopen R. The vignette does not mention a TWITTER_PAT entry gets created in .Renviron. The new version of rtweet does not do this. In my attempts to figure out the 32 error I encountered the https://docs.ropensci.org/rtweet/articles/auth.html page, which did not apply to the older package version of rtweet.

??rtweet_app()

The help page shows this:

USAGE

rtweet_user(api_key = NULL, api_secret = NULL)

rtweet_bot(
  api_key = ask_pass("API key"),
  api_secret = ask_pass("API secret"),
  access_token = ask_pass("access token"),
  access_secret = ask_pass("access token")
)

rtweet_app(bearer_token = ask_pass("bearer token"))

I follow this convention but:

auth <- rtweet_app(bearer_token = ask_pass(Sys.getenv("BEARER_TOKEN")))
Error in ask_pass(Sys.getenv("BEARER_TOKEN")) : 
  could not find function "ask_pass"

However this works (I think):

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

tweets <- search_tweets("#advancedrecycling",
                        include_rts = F,
                        n = 20000,
                        token = auth,
                        lang = "en")

rtweet doesn’t use OAuth 2.0 (yet), so it is not mentioned.

I will modify the documentation to include how often must one authenticate. Currently with rtweet > 0.7.0 you only need to auth_save the token once and then you can use auth_as to use the token saved as much as you want.

rtweet > 0.7.0 does not uses or creates the TWITTER_PAT environment variable. That’s why it is not mentioned.

I’m investigating the issue with ask_pass, but I think you are running rtweet_app on the terminal pasting it instead of using it like this: rtweet_app(), I’ll try to make this easier to understand.

Thanks. It would be good to state OAuth 2.0 is not supported yet. So you are saying I should do the following:

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

auth_save(auth, "my_app")

auth_as("my_app")

Do I need to refer to token = in search_tweets() after I have done this?

Why do you expect OAuth 2.0 to be supported/mentioned? Where did this idea come from? I think it will confuse more the readers/users than help them.

Yes, you just need to save the token once and then on each session (each time you close and reopen R) you can use the same token with auth_as("your_app")

You don’t need to use token = my_token for each call to Twitter API in rtweet if you use the same token loaded with auth_as at the beginning of the R session.

For example:

Session 1:

auth <- rtweet_app(bearer_token = Sys.getenv("BEARER_TOKEN"))
auth_save(auth, "my_app")
auth_as("my_app")
out <- search_tweets("my search")
out2 <- search_tweets("my search2")
q("no")

Session n (or any other session after saving your token):

auth_as("my_app")
out3 <- search_tweets("my search3")

I looked at the Twitter page and see two options. I chose OAuth2.0 to start with. Users get confused by the Twitter page. You need to anticipate that. Hence, my suggestion you mention this. Thanks very much for being so responsive. This is gold.

If users starts looking to Twitter API help they should know better than come to rtweet thinking that everything there is true for rtweet.
The package vignettes and documentation are for users flowing in the opposite direction: they start with R then rtweet and if users have questions go to Twitter API documentation.

I started with rtweet but then wanted to make sure I was getting the benefit from my Twitter Developer API. The rtweet auth vignette says one has to go and define an app on Twitter. That is where the wheels fell off for me. I get where you are coming from but I acted in good faith and am trying to help. I do not mean to irritate you and apologise if I have caused any offense.

It is great that you want to benefit from your Twitter Developer API but not so great how you demand it and what peace you did as if others own you something.

I think you act in good faith, otherwise I wouldn’t have helped, but please take into consideration that the license says that the software is as is without warranty or support. I’m developing and helping in my own time. Being more considerate will help a lot more on my willingness to support you and improve the software.

I hope you’ll get to do what you wanted with rtweet. Let me know if there is any other trouble or confusion.

I am grateful to the OSS community who do amazing things. You have been most helpful. I am learning how to drive rtweet through Datacamp. Hopefully, I can fly solo after this!

1 Like